diff options
| author | bernard.xiong <[email protected]> | 2010-01-21 14:12:40 +0000 |
|---|---|---|
| committer | bernard.xiong <[email protected]> | 2010-01-21 14:12:40 +0000 |
| commit | cefc4f7aa5f819f8f4f5aa9f356d33076f9b0af2 (patch) | |
| tree | c718fc281333497b443bf9c2aba39dba9c2c1fbb /bsp | |
| parent | c28ecae7edc0d9e066c99eb8ad040a73dba56525 (diff) | |
change play thread to static thread. use elm fatfs on radio project.
git-svn-id: https://rt-thread.googlecode.com/svn/trunk@329 bbd45198-f89e-11dd-88c7-29a3b14d5316
Diffstat (limited to 'bsp')
| -rw-r--r-- | bsp/stm32_radio/mp3.c | 2 | ||||
| -rw-r--r-- | bsp/stm32_radio/player_bg.c | 32 | ||||
| -rw-r--r-- | bsp/stm32_radio/project.Opt | 375 | ||||
| -rw-r--r-- | bsp/stm32_radio/project.Uv2 | 14 | ||||
| -rw-r--r-- | bsp/stm32_radio/rtconfig.h | 324 | ||||
| -rw-r--r-- | bsp/stm32_radio/sdcard.c | 6302 | ||||
| -rw-r--r-- | bsp/stm32_radio/sdcard.h | 570 | ||||
| -rw-r--r-- | bsp/stm32_radio/usart.h | 14 |
8 files changed, 3827 insertions, 3806 deletions
diff --git a/bsp/stm32_radio/mp3.c b/bsp/stm32_radio/mp3.c index dc1035a822..e8ad535bd0 100644 --- a/bsp/stm32_radio/mp3.c +++ b/bsp/stm32_radio/mp3.c @@ -381,11 +381,11 @@ void mp3(char* filename) current_offset = 0;
while (mp3_decoder_run(decoder) != -1);
- close(fd);
/* delete decoder object */
mp3_decoder_delete(decoder);
}
+ close(fd);
}
is_playing = RT_FALSE;
}
diff --git a/bsp/stm32_radio/player_bg.c b/bsp/stm32_radio/player_bg.c index e2553c26e7..905fb15682 100644 --- a/bsp/stm32_radio/player_bg.c +++ b/bsp/stm32_radio/player_bg.c @@ -3,8 +3,10 @@ #include <string.h>
-static rt_mq_t player_thread_mq;
rt_bool_t is_playing = RT_FALSE;
+static rt_mq_t player_thread_mq;
+static struct rt_thread player_thread_tid;
+static rt_uint8_t player_thread_stack[0x400];
rt_bool_t player_is_playing()
{
@@ -20,6 +22,15 @@ void player_play_file(const char* fn) /* send to message queue */
rt_mq_send(player_thread_mq, (void*)&request, sizeof(struct player_request));
}
+#ifdef RT_USING_FINSH
+#include <finsh.h>
+static const char _fn[] = "/005.mp3";
+void play()
+{
+ player_play_file(_fn);
+}
+FINSH_FUNCTION_EXPORT(play, play mp3 file test);
+#endif
void player_stop()
{
@@ -49,7 +60,7 @@ void player_thread(void* parameter) /* get music tag information */
mp3(request.fn);
-
+
player_notify_stop();
is_playing = RT_FALSE;
}
@@ -68,16 +79,21 @@ void player_thread(void* parameter) void player_init()
{
- rt_thread_t tid;
+ rt_err_t result;
/* create player thread */
player_thread_mq = rt_mq_create("player", sizeof(struct player_request),
8, RT_IPC_FLAG_FIFO);
RT_ASSERT(player_thread_mq != RT_NULL);
- tid = rt_thread_create("ply_bg", player_thread, RT_NULL,
- 2048, 20, 5);
- if (tid != RT_NULL) rt_thread_startup(tid);
-
- player_ui_init();
+ result = rt_thread_init(&player_thread_tid, "ply_bg", player_thread, RT_NULL,
+ player_thread_stack, sizeof(player_thread_stack),
+ 20, 5);
+
+ if (result != RT_EOK) rt_kprintf("player thread init failed\n");
+ else
+ {
+ rt_thread_startup(&player_thread_tid);
+ player_ui_init();
+ }
}
diff --git a/bsp/stm32_radio/project.Opt b/bsp/stm32_radio/project.Opt index 44d4a82cca..3619890222 100644 --- a/bsp/stm32_radio/project.Opt +++ b/bsp/stm32_radio/project.Opt @@ -11,14 +11,14 @@ DaveTm { 0,0,0,0,0,0,0,0 }
Target (RT-Thread STM32 Radio), 0x0004 // Tools: 'ARM-ADS'
-GRPOPT 1,(Startup),1,0,0
+GRPOPT 1,(Startup),0,0,0
GRPOPT 2,(Kernel),0,0,0
GRPOPT 3,(STM32),0,0,0
GRPOPT 4,(StdPeriph_Driver),0,0,0
GRPOPT 5,(USB-FS-Device_Driver),0,0,0
GRPOPT 6,(CMSIS),0,0,0
GRPOPT 7,(finsh),0,0,0
-GRPOPT 8,(Filesystem),0,0,0
+GRPOPT 8,(Filesystem),1,0,0
GRPOPT 9,(LwIP),0,0,0
GRPOPT 10,(mp3),0,0,0
GRPOPT 11,(RTGUI),0,0,0
@@ -31,8 +31,8 @@ OPTFFF 1,4,1,0,0,0,0,0,<.\application.c><application.c> OPTFFF 1,5,1,0,0,0,0,0,<.\board.c><board.c>
OPTFFF 1,6,1,352321536,0,0,0,0,<.\startup.c><startup.c>
OPTFFF 1,7,1,0,0,0,0,0,<.\stm32f10x_it.c><stm32f10x_it.c>
-OPTFFF 1,8,1,0,0,0,0,0,<.\usart.c><usart.c>
-OPTFFF 1,9,1,402653184,0,0,0,0,<.\sdcard.c><sdcard.c>
+OPTFFF 1,8,1,436207616,0,0,0,0,<.\usart.c><usart.c>
+OPTFFF 1,9,1,301989888,0,0,0,0,<.\sdcard.c><sdcard.c>
OPTFFF 1,10,1,0,0,0,0,0,<.\rtc.c><rtc.c>
OPTFFF 1,11,1,0,0,0,0,0,<.\dm9000.c><dm9000.c>
OPTFFF 1,12,1,889192448,0,0,0,0,<.\fsmc_nand.c><fsmc_nand.c>
@@ -40,7 +40,7 @@ OPTFFF 1,13,1,0,0,0,0,0,<.\fsmc_sram.c><fsmc_sram.c> OPTFFF 1,14,1,0,0,0,0,0,<.\fmt0371\fmt0371.c><fmt0371.c>
OPTFFF 1,15,1,0,0,0,0,0,<.\http.c><http.c>
OPTFFF 1,16,1,0,0,0,0,0,<.\lcd.c><lcd.c>
-OPTFFF 1,17,1,16777216,0,0,0,0,<.\mp3.c><mp3.c>
+OPTFFF 1,17,1,0,0,0,0,0,<.\mp3.c><mp3.c>
OPTFFF 1,18,1,369098752,0,0,0,0,<.\wav.c><wav.c>
OPTFFF 1,19,1,0,0,0,0,0,<.\netbuffer.c><netbuffer.c>
OPTFFF 1,20,1,0,0,0,0,0,<.\key.c><key.c>
@@ -49,194 +49,185 @@ OPTFFF 1,22,1,0,0,0,0,0,<.\filelist.c><filelist.c> OPTFFF 1,23,1,0,0,0,0,0,<.\device_info.c><device_info.c>
OPTFFF 1,24,1,0,0,0,0,0,<.\listview.c><listview.c>
OPTFFF 1,25,1,0,0,0,0,0,<.\player_ui.c><player_ui.c>
-OPTFFF 1,26,1,0,0,0,0,0,<.\player_bg.c><player_bg.c>
+OPTFFF 1,26,1,520093696,0,0,0,0,<.\player_bg.c><player_bg.c>
OPTFFF 1,27,1,469762048,0,0,0,0,<.\play_list.c><play_list.c>
OPTFFF 1,28,1,0,0,0,0,0,<.\ili9325\ili9320.c><ili9320.c>
OPTFFF 1,29,1,0,0,0,0,0,<.\codec.c><codec.c>
-OPTFFF 2,30,1,0,0,0,0,0,<..\..\src\clock.c><clock.c>
-OPTFFF 2,31,1,0,0,0,0,0,<..\..\src\idle.c><idle.c>
-OPTFFF 2,32,1,0,0,0,0,0,<..\..\src\ipc.c><ipc.c>
-OPTFFF 2,33,1,0,0,0,0,0,<..\..\src\mempool.c><mempool.c>
-OPTFFF 2,34,1,0,0,0,0,0,<..\..\src\mem.c><mem.c>
-OPTFFF 2,35,1,0,0,0,0,0,<..\..\src\object.c><object.c>
-OPTFFF 2,36,1,0,0,0,0,0,<..\..\src\scheduler.c><scheduler.c>
-OPTFFF 2,37,1,0,0,0,0,0,<..\..\src\thread.c><thread.c>
-OPTFFF 2,38,1,0,0,0,0,0,<..\..\src\timer.c><timer.c>
-OPTFFF 2,39,1,0,0,0,0,0,<..\..\src\irq.c><irq.c>
-OPTFFF 2,40,1,0,0,0,0,0,<..\..\src\kservice.c><kservice.c>
-OPTFFF 2,41,1,0,0,0,0,0,<..\..\src\device.c><device.c>
-OPTFFF 2,42,1,0,0,0,0,0,<..\..\src\slab.c><slab.c>
-OPTFFF 3,43,1,0,0,0,0,0,<..\..\libcpu\arm\stm32\stack.c><stack.c>
-OPTFFF 3,44,1,0,0,0,0,0,<..\..\libcpu\arm\stm32\interrupt.c><interrupt.c>
-OPTFFF 3,45,1,0,0,0,0,0,<..\..\libcpu\arm\stm32\cpu.c><cpu.c>
-OPTFFF 3,46,1,0,0,0,0,0,<..\..\libcpu\arm\stm32\serial.c><serial.c>
-OPTFFF 3,47,2,0,0,0,0,0,<..\..\libcpu\arm\stm32\context_rvds.S><context_rvds.S>
-OPTFFF 3,48,2,0,0,0,0,0,<..\..\libcpu\arm\stm32\start_rvds.s><start_rvds.s>
-OPTFFF 3,49,1,0,0,0,0,0,<..\..\libcpu\arm\stm32\fault.c><fault.c>
-OPTFFF 3,50,2,0,0,0,0,0,<..\..\libcpu\arm\stm32\fault_rvds.S><fault_rvds.S>
-OPTFFF 4,51,1,0,0,0,0,0,<.\Libraries\STM32F10x_StdPeriph_Driver\src\misc.c><misc.c>
-OPTFFF 4,52,1,0,0,0,0,0,<.\Libraries\STM32F10x_StdPeriph_Driver\src\stm32f10x_adc.c><stm32f10x_adc.c>
-OPTFFF 4,53,1,0,0,0,0,0,<.\Libraries\STM32F10x_StdPeriph_Driver\src\stm32f10x_bkp.c><stm32f10x_bkp.c>
-OPTFFF 4,54,1,0,0,0,0,0,<.\Libraries\STM32F10x_StdPeriph_Driver\src\stm32f10x_can.c><stm32f10x_can.c>
-OPTFFF 4,55,1,0,0,0,0,0,<.\Libraries\STM32F10x_StdPeriph_Driver\src\stm32f10x_crc.c><stm32f10x_crc.c>
-OPTFFF 4,56,1,0,0,0,0,0,<.\Libraries\STM32F10x_StdPeriph_Driver\src\stm32f10x_dac.c><stm32f10x_dac.c>
-OPTFFF 4,57,1,0,0,0,0,0,<.\Libraries\STM32F10x_StdPeriph_Driver\src\stm32f10x_dbgmcu.c><stm32f10x_dbgmcu.c>
-OPTFFF 4,58,1,0,0,0,0,0,<.\Libraries\STM32F10x_StdPeriph_Driver\src\stm32f10x_dma.c><stm32f10x_dma.c>
-OPTFFF 4,59,1,0,0,0,0,0,<.\Libraries\STM32F10x_StdPeriph_Driver\src\stm32f10x_exti.c><stm32f10x_exti.c>
-OPTFFF 4,60,1,0,0,0,0,0,<.\Libraries\STM32F10x_StdPeriph_Driver\src\stm32f10x_flash.c><stm32f10x_flash.c>
-OPTFFF 4,61,1,0,0,0,0,0,<.\Libraries\STM32F10x_StdPeriph_Driver\src\stm32f10x_fsmc.c><stm32f10x_fsmc.c>
-OPTFFF 4,62,1,0,0,0,0,0,<.\Libraries\STM32F10x_StdPeriph_Driver\src\stm32f10x_gpio.c><stm32f10x_gpio.c>
-OPTFFF 4,63,1,0,0,0,0,0,<.\Libraries\STM32F10x_StdPeriph_Driver\src\stm32f10x_i2c.c><stm32f10x_i2c.c>
-OPTFFF 4,64,1,0,0,0,0,0,<.\Libraries\STM32F10x_StdPeriph_Driver\src\stm32f10x_iwdg.c><stm32f10x_iwdg.c>
-OPTFFF 4,65,1,0,0,0,0,0,<.\Libraries\STM32F10x_StdPeriph_Driver\src\stm32f10x_pwr.c><stm32f10x_pwr.c>
-OPTFFF 4,66,1,0,0,0,0,0,<.\Libraries\STM32F10x_StdPeriph_Driver\src\stm32f10x_rcc.c><stm32f10x_rcc.c>
-OPTFFF 4,67,1,0,0,0,0,0,<.\Libraries\STM32F10x_StdPeriph_Driver\src\stm32f10x_rtc.c><stm32f10x_rtc.c>
-OPTFFF 4,68,1,0,0,0,0,0,<.\Libraries\STM32F10x_StdPeriph_Driver\src\stm32f10x_sdio.c><stm32f10x_sdio.c>
-OPTFFF 4,69,1,0,0,0,0,0,<.\Libraries\STM32F10x_StdPeriph_Driver\src\stm32f10x_spi.c><stm32f10x_spi.c>
-OPTFFF 4,70,1,0,0,0,0,0,<.\Libraries\STM32F10x_StdPeriph_Driver\src\stm32f10x_tim.c><stm32f10x_tim.c>
-OPTFFF 4,71,1,0,0,0,0,0,<.\Libraries\STM32F10x_StdPeriph_Driver\src\stm32f10x_usart.c><stm32f10x_usart.c>
-OPTFFF 4,72,1,0,0,0,0,0,<.\Libraries\STM32F10x_StdPeriph_Driver\src\stm32f10x_wwdg.c><stm32f10x_wwdg.c>
-OPTFFF 5,73,1,0,0,0,0,0,<.\Libraries\STM32_USB-FS-Device_Driver\src\otgd_fs_cal.c><otgd_fs_cal.c>
-OPTFFF 5,74,1,0,0,0,0,0,<.\Libraries\STM32_USB-FS-Device_Driver\src\otgd_fs_dev.c><otgd_fs_dev.c>
-OPTFFF 5,75,1,0,0,0,0,0,<.\Libraries\STM32_USB-FS-Device_Driver\src\otgd_fs_int.c><otgd_fs_int.c>
-OPTFFF 5,76,1,0,0,0,0,0,<.\Libraries\STM32_USB-FS-Device_Driver\src\otgd_fs_pcd.c><otgd_fs_pcd.c>
-OPTFFF 5,77,1,0,0,0,0,0,<.\Libraries\STM32_USB-FS-Device_Driver\src\usb_core.c><usb_core.c>
-OPTFFF 5,78,1,0,0,0,0,0,<.\Libraries\STM32_USB-FS-Device_Driver\src\usb_init.c><usb_init.c>
-OPTFFF 5,79,1,0,0,0,0,0,<.\Libraries\STM32_USB-FS-Device_Driver\src\usb_int.c><usb_int.c>
-OPTFFF 5,80,1,0,0,0,0,0,<.\Libraries\STM32_USB-FS-Device_Driver\src\usb_mem.c><usb_mem.c>
-OPTFFF 5,81,1,0,0,0,0,0,<.\Libraries\STM32_USB-FS-Device_Driver\src\usb_regs.c><usb_regs.c>
-OPTFFF 5,82,1,0,0,0,0,0,<.\Libraries\STM32_USB-FS-Device_Driver\src\usb_sil.c><usb_sil.c>
-OPTFFF 6,83,1,0,0,0,0,0,<.\Libraries\CMSIS\Core\CM3\core_cm3.c><core_cm3.c>
-OPTFFF 6,84,1,0,0,0,0,0,<.\Libraries\CMSIS\Core\CM3\system_stm32f10x.c><system_stm32f10x.c>
-OPTFFF 7,85,1,0,0,0,0,0,<..\..\finsh\finsh_compiler.c><finsh_compiler.c>
-OPTFFF 7,86,1,0,0,0,0,0,<..\..\finsh\finsh_error.c><finsh_error.c>
-OPTFFF 7,87,1,0,0,0,0,0,<..\..\finsh\finsh_heap.c><finsh_heap.c>
-OPTFFF 7,88,1,0,0,0,0,0,<..\..\finsh\finsh_init.c><finsh_init.c>
-OPTFFF 7,89,1,0,0,0,0,0,<..\..\finsh\finsh_node.c><finsh_node.c>
-OPTFFF 7,90,1,0,0,0,0,0,<..\..\finsh\finsh_ops.c><finsh_ops.c>
-OPTFFF 7,91,1,0,0,0,0,0,<..\..\finsh\finsh_parser.c><finsh_parser.c>
-OPTFFF 7,92,1,0,0,0,0,0,<..\..\finsh\finsh_token.c><finsh_token.c>
-OPTFFF 7,93,1,0,0,0,0,0,<..\..\finsh\finsh_var.c><finsh_var.c>
-OPTFFF 7,94,1,0,0,0,0,0,<..\..\finsh\finsh_vm.c><finsh_vm.c>
-OPTFFF 7,95,1,0,0,0,0,0,<..\..\finsh\shell.c><shell.c>
-OPTFFF 7,96,1,0,0,0,0,0,<..\..\finsh\symbol.c><symbol.c>
-OPTFFF 7,97,1,0,0,0,0,0,<..\..\finsh\cmd.c><cmd.c>
-OPTFFF 8,98,1,0,0,0,0,0,<..\..\filesystem\dfs\src\dfs_init.c><dfs_init.c>
-OPTFFF 8,99,1,0,0,0,0,0,<..\..\filesystem\dfs\src\dfs_fs.c><dfs_fs.c>
-OPTFFF 8,100,1,0,0,0,0,0,<..\..\filesystem\dfs\src\dfs_raw.c><dfs_raw.c>
-OPTFFF 8,101,1,0,0,0,0,0,<..\..\filesystem\dfs\src\dfs_util.c><dfs_util.c>
-OPTFFF 8,102,1,0,0,0,0,0,<..\..\filesystem\dfs\src\dfs_posix.c><dfs_posix.c>
-OPTFFF 8,103,1,0,0,0,0,0,<..\..\filesystem\dfs\filesystems\efsl\src\fs\vfat\dir.c><dir.c>
-OPTFFF 8,104,1,0,0,0,0,0,<..\..\filesystem\dfs\filesystems\efsl\src\fs\vfat\fat.c><fat.c>
-OPTFFF 8,105,1,0,0,0,0,0,<..\..\filesystem\dfs\filesystems\efsl\src\fs\vfat\file.c><file.c>
-OPTFFF 8,106,1,0,0,0,0,0,<..\..\filesystem\dfs\filesystems\efsl\src\fs\vfat\fs.c><fs.c>
-OPTFFF 8,107,1,0,0,0,0,0,<..\..\filesystem\dfs\filesystems\efsl\src\fs\vfat\ls.c><ls.c>
-OPTFFF 8,108,1,0,0,0,0,0,<..\..\filesystem\dfs\filesystems\efsl\src\fs\vfat\time.c><time.c>
-OPTFFF 8,109,1,0,0,0,0,0,<..\..\filesystem\dfs\filesystems\efsl\src\fs\vfat\ui.c><ui.c>
-OPTFFF 8,110,1,0,0,0,0,0,<..\..\filesystem\dfs\filesystems\efsl\src\base\plibc.c><plibc.c>
-OPTFFF 8,111,1,0,0,0,0,0,<..\..\filesystem\dfs\filesystems\efsl\src\base\efs.c><efs.c>
-OPTFFF 8,112,1,0,0,0,0,0,<..\..\filesystem\dfs\filesystems\efsl\src\base\extract.c><extract.c>
-OPTFFF 8,113,1,0,0,0,0,0,<..\..\filesystem\dfs\filesystems\efsl\src\base\partition.c><partition.c>
-OPTFFF 8,114,1,0,0,0,0,0,<..\..\filesystem\dfs\src\dfs_cache.c><dfs_cache.c>
-OPTFFF 9,115,1,0,0,0,0,0,<..\..\net\lwip\src\core\dhcp.c><dhcp.c>
-OPTFFF 9,116,1,0,0,0,0,0,<..\..\net\lwip\src\core\dns.c><dns.c>
-OPTFFF 9,117,1,0,0,0,0,0,<..\..\net\lwip\src\core\init.c><init.c>
-OPTFFF 9,118,1,0,0,0,0,0,<..\..\net\lwip\src\core\memp.c><memp.c>
-OPTFFF 9,119,1,0,0,0,0,0,<..\..\net\lwip\src\core\netif.c><netif.c>
-OPTFFF 9,120,1,0,0,0,0,0,<..\..\net\lwip\src\core\pbuf.c><pbuf.c>
-OPTFFF 9,121,1,0,0,0,0,0,<..\..\net\lwip\src\core\raw.c><raw.c>
-OPTFFF 9,122,1,0,0,0,0,0,<..\..\net\lwip\src\core\stats.c><stats.c>
-OPTFFF 9,123,1,0,0,0,0,0,<..\..\net\lwip\src\core\sys.c><sys.c>
-OPTFFF 9,124,1,0,0,0,0,0,<..\..\net\lwip\src\core\tcp.c><tcp.c>
-OPTFFF 9,125,1,0,0,0,0,0,<..\..\net\lwip\src\core\tcp_in.c><tcp_in.c>
-OPTFFF 9,126,1,0,0,0,0,0,<..\..\net\lwip\src\core\tcp_out.c><tcp_out.c>
-OPTFFF 9,127,1,0,0,0,0,0,<..\..\net\lwip\src\core\udp.c><udp.c>
-OPTFFF 9,128,1,0,0,0,0,0,<..\..\net\lwip\src\core\ipv4\autoip.c><autoip.c>
-OPTFFF 9,129,1,0,0,0,0,0,<..\..\net\lwip\src\core\ipv4\icmp.c><icmp.c>
-OPTFFF 9,130,1,0,0,0,0,0,<..\..\net\lwip\src\core\ipv4\igmp.c><igmp.c>
-OPTFFF 9,131,1,0,0,0,0,0,<..\..\net\lwip\src\core\ipv4\inet.c><inet.c>
-OPTFFF 9,132,1,0,0,0,0,0,<..\..\net\lwip\src\core\ipv4\inet_chksum.c><inet_chksum.c>
-OPTFFF 9,133,1,0,0,0,0,0,<..\..\net\lwip\src\core\ipv4\ip.c><ip.c>
-OPTFFF 9,134,1,0,0,0,0,0,<..\..\net\lwip\src\core\ipv4\ip_addr.c><ip_addr.c>
-OPTFFF 9,135,1,0,0,0,0,0,<..\..\net\lwip\src\core\ipv4\ip_frag.c><ip_frag.c>
-OPTFFF 9,136,1,0,0,0,0,0,<..\..\net\lwip\src\core\snmp\msg_in.c><msg_in.c>
-OPTFFF 9,137,1,0,0,0,0,0,<..\..\net\lwip\src\core\snmp\msg_out.c><msg_out.c>
-OPTFFF 9,138,1,0,0,0,0,0,<..\..\net\lwip\src\api\api_lib.c><api_lib.c>
-OPTFFF 9,139,1,0,0,0,0,0,<..\..\net\lwip\src\api\api_msg.c><api_msg.c>
-OPTFFF 9,140,1,0,0,0,0,0,<..\..\net\lwip\src\api\err.c><err.c>
-OPTFFF 9,141,1,0,0,0,0,0,<..\..\net\lwip\src\api\netbuf.c><netbuf.c>
-OPTFFF 9,142,1,0,0,0,0,0,<..\..\net\lwip\src\api\netdb.c><netdb.c>
-OPTFFF 9,143,1,0,0,0,0,0,<..\..\net\lwip\src\api\netifapi.c><netifapi.c>
-OPTFFF 9,144,1,0,0,0,0,0,<..\..\net\lwip\src\api\tcpip.c><tcpip.c>
-OPTFFF 9,145,1,0,0,0,0,0,<..\..\net\lwip\src\netif\etharp.c><etharp.c>
-OPTFFF 9,146,1,0,0,0,0,0,<..\..\net\lwip\src\netif\ethernetif.c><ethernetif.c>
-OPTFFF 9,147,1,0,0,0,0,0,<..\..\net\lwip\src\netif\loopif.c><loopif.c>
-OPTFFF 9,148,1,0,0,0,0,0,<..\..\net\lwip\src\arch\sys_arch_init.c><sys_arch_init.c>
-OPTFFF 9,149,1,0,0,0,0,0,<..\..\net\lwip\src\arch\sys_arch.c><sys_arch.c>
-OPTFFF 9,150,1,0,0,0,0,0,<..\..\net\lwip\src\api\sockets.c><sockets.c>
-OPTFFF 10,151,1,0,0,0,0,0,<.\mp3\mp3dec.c><mp3dec.c>
-OPTFFF 10,152,1,0,0,0,0,0,<.\mp3\mp3tabs.c><mp3tabs.c>
-OPTFFF 10,153,1,0,0,0,0,0,<.\mp3\real\bitstream.c><bitstream.c>
-OPTFFF 10,154,1,0,0,0,0,0,<.\mp3\real\buffers.c><buffers.c>
-OPTFFF 10,155,1,0,0,0,0,0,<.\mp3\real\dct32.c><dct32.c>
-OPTFFF 10,156,1,0,0,0,0,0,<.\mp3\real\dequant.c><dequant.c>
-OPTFFF 10,157,1,0,0,0,0,0,<.\mp3\real\dqchan.c><dqchan.c>
-OPTFFF 10,158,1,0,0,0,0,0,<.\mp3\real\huffman.c><huffman.c>
-OPTFFF 10,159,1,0,0,0,0,0,<.\mp3\real\hufftabs.c><hufftabs.c>
-OPTFFF 10,160,1,0,0,0,0,0,<.\mp3\real\imdct.c><imdct.c>
-OPTFFF 10,161,1,0,0,0,0,0,<.\mp3\real\scalfact.c><scalfact.c>
-OPTFFF 10,162,1,0,0,0,0,0,<.\mp3\real\stproc.c><stproc.c>
-OPTFFF 10,163,1,0,0,0,0,0,<.\mp3\real\subband.c><subband.c>
-OPTFFF 10,164,1,0,0,0,0,0,<.\mp3\real\trigtabs.c><trigtabs.c>
-OPTFFF 10,165,2,0,0,0,0,0,<.\mp3\real\arm\asmpoly_thumb2.s><asmpoly_thumb2.s>
-OPTFFF 10,166,2,0,0,0,0,0,<.\mp3\real\arm\asmmisc.s><asmmisc.s>
-OPTFFF 11,167,1,0,0,0,0,0,<..\..\rtgui\common\rtgui_object.c><rtgui_object.c>
-OPTFFF 11,168,1,0,0,0,0,0,<..\..\rtgui\common\rtgui_system.c><rtgui_system.c>
-OPTFFF 11,169,1,0,0,0,0,0,<..\..\rtgui\common\rtgui_theme.c><rtgui_theme.c>
-OPTFFF 11,170,1,0,0,0,0,0,<..\..\rtgui\common\asc12font.c><asc12font.c>
-OPTFFF 11,171,1,0,0,0,0,0,<..\..\rtgui\common\asc16font.c><asc16font.c>
-OPTFFF 11,172,1,0,0,0,0,0,<..\..\rtgui\common\color.c><color.c>
-OPTFFF 11,173,1,0,0,0,0,0,<..\..\rtgui\common\dc.c><dc.c>
-OPTFFF 11,174,1,0,0,0,0,0,<..\..\rtgui\common\dc_buffer.c><dc_buffer.c>
-OPTFFF 11,175,1,0,0,0,0,0,<..\..\rtgui\common\dc_hw.c><dc_hw.c>
-OPTFFF 11,176,1,0,0,0,0,0,<..\..\rtgui\common\filerw.c><filerw.c>
-OPTFFF 11,177,1,402653184,0,0,0,0,<..\..\rtgui\common\font.c><font.c>
-OPTFFF 11,178,1,0,0,0,0,0,<..\..\rtgui\common\image.c><image.c>
-OPTFFF 11,179,1,0,0,0,0,0,<..\..\rtgui\common\image_xpm.c><image_xpm.c>
-OPTFFF 11,180,1,0,0,0,0,0,<..\..\rtgui\common\image_hdc.c><image_hdc.c>
-OPTFFF 11,181,1,0,0,0,0,0,<..\..\rtgui\common\region.c><region.c>
-OPTFFF 11,182,1,0,0,0,0,0,<..\..\rtgui\server\server.c><server.c>
-OPTFFF 11,183,1,0,0,0,0,0,<..\..\rtgui\server\driver.c><driver.c>
-OPTFFF 11,184,1,0,0,0,0,0,<..\..\rtgui\server\panel.c><panel.c>
-OPTFFF 11,185,1,0,0,0,0,0,<..\..\rtgui\widgets\widget.c><widget.c>
-OPTFFF 11,186,1,0,0,0,0,0,<..\..\rtgui\widgets\window.c><window.c>
-OPTFFF 11,187,1,0,0,0,0,0,<..\..\rtgui\widgets\workbench.c><workbench.c>
-OPTFFF 11,188,1,0,0,0,0,0,<..\..\rtgui\widgets\view.c><view.c>
-OPTFFF 11,189,1,0,0,0,0,0,<..\..\rtgui\widgets\box.c><box.c>
-OPTFFF 11,190,1,0,0,0,0,0,<..\..\rtgui\widgets\button.c><button.c>
-OPTFFF 11,191,1,0,0,0,0,0,<..\..\rtgui\widgets\container.c><container.c>
-OPTFFF 11,192,1,0,0,0,0,0,<..\..\rtgui\widgets\iconbox.c><iconbox.c>
-OPTFFF 11,193,1,0,0,0,0,0,<..\..\rtgui\widgets\label.c><label.c>
-OPTFFF 11,194,1,0,0,0,0,0,<..\..\rtgui\widgets\textbox.c><textbox.c>
-OPTFFF 11,195,1,0,0,0,0,0,<..\..\rtgui\widgets\title.c><title.c>
-OPTFFF 11,196,1,0,0,0,0,0,<..\..\rtgui\widgets\toplevel.c><toplevel.c>
-OPTFFF 11,197,1,0,0,0,0,0,<..\..\rtgui\server\mouse.c><mouse.c>
-OPTFFF 11,198,1,0,0,0,0,0,<..\..\rtgui\server\topwin.c><topwin.c>
-OPTFFF 11,199,1,0,0,0,0,0,<..\..\rtgui\common\font_hz_file.c><font_hz_file.c>
-OPTFFF 11,200,1,0,0,0,0,0,<..\..\rtgui\common\hz16font.c><hz16font.c>
-OPTFFF 11,201,1,0,0,0,0,0,<..\..\rtgui\common\hz12font.c><hz12font.c>
-OPTFFF 12,202,1,0,0,0,0,0,<.\Libraries\Mass_Storage\src\hw_config.c><hw_config.c>
-OPTFFF 12,203,1,0,0,0,0,0,<.\Libraries\Mass_Storage\src\mass_mal.c><mass_mal.c>
-OPTFFF 12,204,1,419430400,0,0,0,0,<.\Libraries\Mass_Storage\src\memory.c><memory.c>
-OPTFFF 12,205,1,0,0,0,0,0,<.\Libraries\Mass_Storage\src\usb_bot.c><usb_bot.c>
-OPTFFF 12,206,1,100663296,0,0,0,0,<.\Libraries\Mass_Storage\src\scsi_data.c><scsi_data.c>
-OPTFFF 12,207,1,0,0,0,0,0,<.\Libraries\Mass_Storage\src\usb_desc.c><usb_desc.c>
-OPTFFF 12,208,1,0,0,0,0,0,<.\Libraries\Mass_Storage\src\usb_endp.c><usb_endp.c>
-OPTFFF 12,209,1,0,0,0,0,0,<.\Libraries\Mass_Storage\src\usb_istr.c><usb_istr.c>
-OPTFFF 12,210,1,0,0,0,0,0,<.\Libraries\Mass_Storage\src\usb_prop.c><usb_prop.c>
-OPTFFF 12,211,1,0,0,0,0,0,<.\Libraries\Mass_Storage\src\usb_pwr.c><usb_pwr.c>
-OPTFFF 12,212,1,0,0,0,0,0,<.\Libraries\Mass_Storage\src\usb_scsi.c><usb_scsi.c>
-OPTFFF 12,213,1,16777216,0,0,0,0,<.\Libraries\Mass_Storage\src\usb_main.c><usb_main.c>
+OPTFFF 1,30,1,0,0,0,0,0,<.\spi_flash.c><spi_flash.c>
+OPTFFF 2,31,1,0,0,0,0,0,<..\..\src\clock.c><clock.c>
+OPTFFF 2,32,1,0,0,0,0,0,<..\..\src\idle.c><idle.c>
+OPTFFF 2,33,1,0,0,0,0,0,<..\..\src\ipc.c><ipc.c>
+OPTFFF 2,34,1,0,0,0,0,0,<..\..\src\mempool.c><mempool.c>
+OPTFFF 2,35,1,0,0,0,0,0,<..\..\src\mem.c><mem.c>
+OPTFFF 2,36,1,0,0,0,0,0,<..\..\src\object.c><object.c>
+OPTFFF 2,37,1,0,0,0,0,0,<..\..\src\scheduler.c><scheduler.c>
+OPTFFF 2,38,1,0,0,0,0,0,<..\..\src\thread.c><thread.c>
+OPTFFF 2,39,1,0,0,0,0,0,<..\..\src\timer.c><timer.c>
+OPTFFF 2,40,1,0,0,0,0,0,<..\..\src\irq.c><irq.c>
+OPTFFF 2,41,1,0,0,0,0,0,<..\..\src\kservice.c><kservice.c>
+OPTFFF 2,42,1,0,0,0,0,0,<..\..\src\device.c><device.c>
+OPTFFF 2,43,1,0,0,0,0,0,<..\..\src\slab.c><slab.c>
+OPTFFF 3,44,1,0,0,0,0,0,<..\..\libcpu\arm\stm32\stack.c><stack.c>
+OPTFFF 3,45,1,0,0,0,0,0,<..\..\libcpu\arm\stm32\interrupt.c><interrupt.c>
+OPTFFF 3,46,1,0,0,0,0,0,<..\..\libcpu\arm\stm32\cpu.c><cpu.c>
+OPTFFF 3,47,1,0,0,0,0,0,<..\..\libcpu\arm\stm32\serial.c><serial.c>
+OPTFFF 3,48,2,0,0,0,0,0,<..\..\libcpu\arm\stm32\context_rvds.S><context_rvds.S>
+OPTFFF 3,49,2,0,0,0,0,0,<..\..\libcpu\arm\stm32\start_rvds.s><start_rvds.s>
+OPTFFF 3,50,1,0,0,0,0,0,<..\..\libcpu\arm\stm32\fault.c><fault.c>
+OPTFFF 3,51,2,0,0,0,0,0,<..\..\libcpu\arm\stm32\fault_rvds.S><fault_rvds.S>
+OPTFFF 4,52,1,0,0,0,0,0,<.\Libraries\STM32F10x_StdPeriph_Driver\src\misc.c><misc.c>
+OPTFFF 4,53,1,0,0,0,0,0,<.\Libraries\STM32F10x_StdPeriph_Driver\src\stm32f10x_adc.c><stm32f10x_adc.c>
+OPTFFF 4,54,1,0,0,0,0,0,<.\Libraries\STM32F10x_StdPeriph_Driver\src\stm32f10x_bkp.c><stm32f10x_bkp.c>
+OPTFFF 4,55,1,0,0,0,0,0,<.\Libraries\STM32F10x_StdPeriph_Driver\src\stm32f10x_can.c><stm32f10x_can.c>
+OPTFFF 4,56,1,0,0,0,0,0,<.\Libraries\STM32F10x_StdPeriph_Driver\src\stm32f10x_crc.c><stm32f10x_crc.c>
+OPTFFF 4,57,1,0,0,0,0,0,<.\Libraries\STM32F10x_StdPeriph_Driver\src\stm32f10x_dac.c><stm32f10x_dac.c>
+OPTFFF 4,58,1,0,0,0,0,0,<.\Libraries\STM32F10x_StdPeriph_Driver\src\stm32f10x_dbgmcu.c><stm32f10x_dbgmcu.c>
+OPTFFF 4,59,1,0,0,0,0,0,<.\Libraries\STM32F10x_StdPeriph_Driver\src\stm32f10x_dma.c><stm32f10x_dma.c>
+OPTFFF 4,60,1,0,0,0,0,0,<.\Libraries\STM32F10x_StdPeriph_Driver\src\stm32f10x_exti.c><stm32f10x_exti.c>
+OPTFFF 4,61,1,0,0,0,0,0,<.\Libraries\STM32F10x_StdPeriph_Driver\src\stm32f10x_flash.c><stm32f10x_flash.c>
+OPTFFF 4,62,1,0,0,0,0,0,<.\Libraries\STM32F10x_StdPeriph_Driver\src\stm32f10x_fsmc.c><stm32f10x_fsmc.c>
+OPTFFF 4,63,1,0,0,0,0,0,<.\Libraries\STM32F10x_StdPeriph_Driver\src\stm32f10x_gpio.c><stm32f10x_gpio.c>
+OPTFFF 4,64,1,0,0,0,0,0,<.\Libraries\STM32F10x_StdPeriph_Driver\src\stm32f10x_i2c.c><stm32f10x_i2c.c>
+OPTFFF 4,65,1,0,0,0,0,0,<.\Libraries\STM32F10x_StdPeriph_Driver\src\stm32f10x_iwdg.c><stm32f10x_iwdg.c>
+OPTFFF 4,66,1,0,0,0,0,0,<.\Libraries\STM32F10x_StdPeriph_Driver\src\stm32f10x_pwr.c><stm32f10x_pwr.c>
+OPTFFF 4,67,1,0,0,0,0,0,<.\Libraries\STM32F10x_StdPeriph_Driver\src\stm32f10x_rcc.c><stm32f10x_rcc.c>
+OPTFFF 4,68,1,0,0,0,0,0,<.\Libraries\STM32F10x_StdPeriph_Driver\src\stm32f10x_rtc.c><stm32f10x_rtc.c>
+OPTFFF 4,69,1,0,0,0,0,0,<.\Libraries\STM32F10x_StdPeriph_Driver\src\stm32f10x_sdio.c><stm32f10x_sdio.c>
+OPTFFF 4,70,1,0,0,0,0,0,<.\Libraries\STM32F10x_StdPeriph_Driver\src\stm32f10x_spi.c><stm32f10x_spi.c>
+OPTFFF 4,71,1,0,0,0,0,0,<.\Libraries\STM32F10x_StdPeriph_Driver\src\stm32f10x_tim.c><stm32f10x_tim.c>
+OPTFFF 4,72,1,0,0,0,0,0,<.\Libraries\STM32F10x_StdPeriph_Driver\src\stm32f10x_usart.c><stm32f10x_usart.c>
+OPTFFF 4,73,1,0,0,0,0,0,<.\Libraries\STM32F10x_StdPeriph_Driver\src\stm32f10x_wwdg.c><stm32f10x_wwdg.c>
+OPTFFF 5,74,1,0,0,0,0,0,<.\Libraries\STM32_USB-FS-Device_Driver\src\otgd_fs_cal.c><otgd_fs_cal.c>
+OPTFFF 5,75,1,0,0,0,0,0,<.\Libraries\STM32_USB-FS-Device_Driver\src\otgd_fs_dev.c><otgd_fs_dev.c>
+OPTFFF 5,76,1,0,0,0,0,0,<.\Libraries\STM32_USB-FS-Device_Driver\src\otgd_fs_int.c><otgd_fs_int.c>
+OPTFFF 5,77,1,0,0,0,0,0,<.\Libraries\STM32_USB-FS-Device_Driver\src\otgd_fs_pcd.c><otgd_fs_pcd.c>
+OPTFFF 5,78,1,0,0,0,0,0,<.\Libraries\STM32_USB-FS-Device_Driver\src\usb_core.c><usb_core.c>
+OPTFFF 5,79,1,0,0,0,0,0,<.\Libraries\STM32_USB-FS-Device_Driver\src\usb_init.c><usb_init.c>
+OPTFFF 5,80,1,0,0,0,0,0,<.\Libraries\STM32_USB-FS-Device_Driver\src\usb_int.c><usb_int.c>
+OPTFFF 5,81,1,0,0,0,0,0,<.\Libraries\STM32_USB-FS-Device_Driver\src\usb_mem.c><usb_mem.c>
+OPTFFF 5,82,1,0,0,0,0,0,<.\Libraries\STM32_USB-FS-Device_Driver\src\usb_regs.c><usb_regs.c>
+OPTFFF 5,83,1,0,0,0,0,0,<.\Libraries\STM32_USB-FS-Device_Driver\src\usb_sil.c><usb_sil.c>
+OPTFFF 6,84,1,0,0,0,0,0,<.\Libraries\CMSIS\Core\CM3\core_cm3.c><core_cm3.c>
+OPTFFF 6,85,1,0,0,0,0,0,<.\Libraries\CMSIS\Core\CM3\system_stm32f10x.c><system_stm32f10x.c>
+OPTFFF 7,86,1,0,0,0,0,0,<..\..\finsh\finsh_compiler.c><finsh_compiler.c>
+OPTFFF 7,87,1,0,0,0,0,0,<..\..\finsh\finsh_error.c><finsh_error.c>
+OPTFFF 7,88,1,0,0,0,0,0,<..\..\finsh\finsh_heap.c><finsh_heap.c>
+OPTFFF 7,89,1,0,0,0,0,0,<..\..\finsh\finsh_init.c><finsh_init.c>
+OPTFFF 7,90,1,0,0,0,0,0,<..\..\finsh\finsh_node.c><finsh_node.c>
+OPTFFF 7,91,1,0,0,0,0,0,<..\..\finsh\finsh_ops.c><finsh_ops.c>
+OPTFFF 7,92,1,0,0,0,0,0,<..\..\finsh\finsh_parser.c><finsh_parser.c>
+OPTFFF 7,93,1,0,0,0,0,0,<..\..\finsh\finsh_token.c><finsh_token.c>
+OPTFFF 7,94,1,0,0,0,0,0,<..\..\finsh\finsh_var.c><finsh_var.c>
+OPTFFF 7,95,1,0,0,0,0,0,<..\..\finsh\finsh_vm.c><finsh_vm.c>
+OPTFFF 7,96,1,0,0,0,0,0,<..\..\finsh\shell.c><shell.c>
+OPTFFF 7,97,1,0,0,0,0,0,<..\..\finsh\symbol.c><symbol.c>
+OPTFFF 7,98,1,0,0,0,0,0,<..\..\finsh\cmd.c><cmd.c>
+OPTFFF 8,99,1,0,0,0,0,0,<..\..\filesystem\dfs\src\dfs_init.c><dfs_init.c>
+OPTFFF 8,100,1,0,0,0,0,0,<..\..\filesystem\dfs\src\dfs_fs.c><dfs_fs.c>
+OPTFFF 8,101,1,0,0,0,0,0,<..\..\filesystem\dfs\src\dfs_raw.c><dfs_raw.c>
+OPTFFF 8,102,1,0,0,0,0,0,<..\..\filesystem\dfs\src\dfs_util.c><dfs_util.c>
+OPTFFF 8,103,1,0,0,0,0,0,<..\..\filesystem\dfs\src\dfs_posix.c><dfs_posix.c>
+OPTFFF 8,104,1,0,0,0,0,0,<..\..\filesystem\dfs\filesystems\elmfat\dfs_elm.c><dfs_elm.c>
+OPTFFF 8,105,1,0,0,0,0,0,<..\..\filesystem\dfs\filesystems\elmfat\ff.c><ff.c>
+OPTFFF 9,106,1,0,0,0,0,0,<..\..\net\lwip\src\core\dhcp.c><dhcp.c>
+OPTFFF 9,107,1,0,0,0,0,0,<..\..\net\lwip\src\core\dns.c><dns.c>
+OPTFFF 9,108,1,0,0,0,0,0,<..\..\net\lwip\src\core\init.c><init.c>
+OPTFFF 9,109,1,0,0,0,0,0,<..\..\net\lwip\src\core\memp.c><memp.c>
+OPTFFF 9,110,1,0,0,0,0,0,<..\..\net\lwip\src\core\netif.c><netif.c>
+OPTFFF 9,111,1,0,0,0,0,0,<..\..\net\lwip\src\core\pbuf.c><pbuf.c>
+OPTFFF 9,112,1,0,0,0,0,0,<..\..\net\lwip\src\core\raw.c><raw.c>
+OPTFFF 9,113,1,0,0,0,0,0,<..\..\net\lwip\src\core\stats.c><stats.c>
+OPTFFF 9,114,1,0,0,0,0,0,<..\..\net\lwip\src\core\sys.c><sys.c>
+OPTFFF 9,115,1,0,0,0,0,0,<..\..\net\lwip\src\core\tcp.c><tcp.c>
+OPTFFF 9,116,1,0,0,0,0,0,<..\..\net\lwip\src\core\tcp_in.c><tcp_in.c>
+OPTFFF 9,117,1,0,0,0,0,0,<..\..\net\lwip\src\core\tcp_out.c><tcp_out.c>
+OPTFFF 9,118,1,0,0,0,0,0,<..\..\net\lwip\src\core\udp.c><udp.c>
+OPTFFF 9,119,1,0,0,0,0,0,<..\..\net\lwip\src\core\ipv4\autoip.c><autoip.c>
+OPTFFF 9,120,1,0,0,0,0,0,<..\..\net\lwip\src\core\ipv4\icmp.c><icmp.c>
+OPTFFF 9,121,1,0,0,0,0,0,<..\..\net\lwip\src\core\ipv4\igmp.c><igmp.c>
+OPTFFF 9,122,1,0,0,0,0,0,<..\..\net\lwip\src\core\ipv4\inet.c><inet.c>
+OPTFFF 9,123,1,0,0,0,0,0,<..\..\net\lwip\src\core\ipv4\inet_chksum.c><inet_chksum.c>
+OPTFFF 9,124,1,0,0,0,0,0,<..\..\net\lwip\src\core\ipv4\ip.c><ip.c>
+OPTFFF 9,125,1,0,0,0,0,0,<..\..\net\lwip\src\core\ipv4\ip_addr.c><ip_addr.c>
+OPTFFF 9,126,1,0,0,0,0,0,<..\..\net\lwip\src\core\ipv4\ip_frag.c><ip_frag.c>
+OPTFFF 9,127,1,0,0,0,0,0,<..\..\net\lwip\src\core\snmp\msg_in.c><msg_in.c>
+OPTFFF 9,128,1,0,0,0,0,0,<..\..\net\lwip\src\core\snmp\msg_out.c><msg_out.c>
+OPTFFF 9,129,1,0,0,0,0,0,<..\..\net\lwip\src\api\api_lib.c><api_lib.c>
+OPTFFF 9,130,1,0,0,0,0,0,<..\..\net\lwip\src\api\api_msg.c><api_msg.c>
+OPTFFF 9,131,1,0,0,0,0,0,<..\..\net\lwip\src\api\err.c><err.c>
+OPTFFF 9,132,1,0,0,0,0,0,<..\..\net\lwip\src\api\netbuf.c><netbuf.c>
+OPTFFF 9,133,1,0,0,0,0,0,<..\..\net\lwip\src\api\netdb.c><netdb.c>
+OPTFFF 9,134,1,0,0,0,0,0,<..\..\net\lwip\src\api\netifapi.c><netifapi.c>
+OPTFFF 9,135,1,0,0,0,0,0,<..\..\net\lwip\src\api\tcpip.c><tcpip.c>
+OPTFFF 9,136,1,0,0,0,0,0,<..\..\net\lwip\src\netif\etharp.c><etharp.c>
+OPTFFF 9,137,1,0,0,0,0,0,<..\..\net\lwip\src\netif\ethernetif.c><ethernetif.c>
+OPTFFF 9,138,1,0,0,0,0,0,<..\..\net\lwip\src\netif\loopif.c><loopif.c>
+OPTFFF 9,139,1,0,0,0,0,0,<..\..\net\lwip\src\arch\sys_arch_init.c><sys_arch_init.c>
+OPTFFF 9,140,1,0,0,0,0,0,<..\..\net\lwip\src\arch\sys_arch.c><sys_arch.c>
+OPTFFF 9,141,1,0,0,0,0,0,<..\..\net\lwip\src\api\sockets.c><sockets.c>
+OPTFFF 10,142,1,0,0,0,0,0,<.\mp3\mp3dec.c><mp3dec.c>
+OPTFFF 10,143,1,0,0,0,0,0,<.\mp3\mp3tabs.c><mp3tabs.c>
+OPTFFF 10,144,1,0,0,0,0,0,<.\mp3\real\bitstream.c><bitstream.c>
+OPTFFF 10,145,1,0,0,0,0,0,<.\mp3\real\buffers.c><buffers.c>
+OPTFFF 10,146,1,0,0,0,0,0,<.\mp3\real\dct32.c><dct32.c>
+OPTFFF 10,147,1,0,0,0,0,0,<.\mp3\real\dequant.c><dequant.c>
+OPTFFF 10,148,1,0,0,0,0,0,<.\mp3\real\dqchan.c><dqchan.c>
+OPTFFF 10,149,1,0,0,0,0,0,<.\mp3\real\huffman.c><huffman.c>
+OPTFFF 10,150,1,0,0,0,0,0,<.\mp3\real\hufftabs.c><hufftabs.c>
+OPTFFF 10,151,1,0,0,0,0,0,<.\mp3\real\imdct.c><imdct.c>
+OPTFFF 10,152,1,0,0,0,0,0,<.\mp3\real\scalfact.c><scalfact.c>
+OPTFFF 10,153,1,0,0,0,0,0,<.\mp3\real\stproc.c><stproc.c>
+OPTFFF 10,154,1,0,0,0,0,0,<.\mp3\real\subband.c><subband.c>
+OPTFFF 10,155,1,0,0,0,0,0,<.\mp3\real\trigtabs.c><trigtabs.c>
+OPTFFF 10,156,2,0,0,0,0,0,<.\mp3\real\arm\asmpoly_thumb2.s><asmpoly_thumb2.s>
+OPTFFF 10,157,2,0,0,0,0,0,<.\mp3\real\arm\asmmisc.s><asmmisc.s>
+OPTFFF 11,158,1,0,0,0,0,0,<..\..\rtgui\common\rtgui_object.c><rtgui_object.c>
+OPTFFF 11,159,1,0,0,0,0,0,<..\..\rtgui\common\rtgui_system.c><rtgui_system.c>
+OPTFFF 11,160,1,0,0,0,0,0,<..\..\rtgui\common\rtgui_theme.c><rtgui_theme.c>
+OPTFFF 11,161,1,0,0,0,0,0,<..\..\rtgui\common\asc12font.c><asc12font.c>
+OPTFFF 11,162,1,0,0,0,0,0,<..\..\rtgui\common\asc16font.c><asc16font.c>
+OPTFFF 11,163,1,0,0,0,0,0,<..\..\rtgui\common\color.c><color.c>
+OPTFFF 11,164,1,0,0,0,0,0,<..\..\rtgui\common\dc.c><dc.c>
+OPTFFF 11,165,1,0,0,0,0,0,<..\..\rtgui\common\dc_buffer.c><dc_buffer.c>
+OPTFFF 11,166,1,0,0,0,0,0,<..\..\rtgui\common\dc_hw.c><dc_hw.c>
+OPTFFF 11,167,1,0,0,0,0,0,<..\..\rtgui\common\filerw.c><filerw.c>
+OPTFFF 11,168,1,0,0,0,0,0,<..\..\rtgui\common\font.c><font.c>
+OPTFFF 11,169,1,0,0,0,0,0,<..\..\rtgui\common\image.c><image.c>
+OPTFFF 11,170,1,0,0,0,0,0,<..\..\rtgui\common\image_xpm.c><image_xpm.c>
+OPTFFF 11,171,1,0,0,0,0,0,<..\..\rtgui\common\image_hdc.c><image_hdc.c>
+OPTFFF 11,172,1,0,0,0,0,0,<..\..\rtgui\common\region.c><region.c>
+OPTFFF 11,173,1,0,0,0,0,0,<..\..\rtgui\server\server.c><server.c>
+OPTFFF 11,174,1,0,0,0,0,0,<..\..\rtgui\server\driver.c><driver.c>
+OPTFFF 11,175,1,0,0,0,0,0,<..\..\rtgui\server\panel.c><panel.c>
+OPTFFF 11,176,1,0,0,0,0,0,<..\..\rtgui\widgets\widget.c><widget.c>
+OPTFFF 11,177,1,0,0,0,0,0,<..\..\rtgui\widgets\window.c><window.c>
+OPTFFF 11,178,1,0,0,0,0,0,<..\..\rtgui\widgets\workbench.c><workbench.c>
+OPTFFF 11,179,1,0,0,0,0,0,<..\..\rtgui\widgets\view.c><view.c>
+OPTFFF 11,180,1,0,0,0,0,0,<..\..\rtgui\widgets\box.c><box.c>
+OPTFFF 11,181,1,0,0,0,0,0,<..\..\rtgui\widgets\button.c><button.c>
+OPTFFF 11,182,1,0,0,0,0,0,<..\..\rtgui\widgets\container.c><container.c>
+OPTFFF 11,183,1,0,0,0,0,0,<..\..\rtgui\widgets\iconbox.c><iconbox.c>
+OPTFFF 11,184,1,0,0,0,0,0,<..\..\rtgui\widgets\label.c><label.c>
+OPTFFF 11,185,1,0,0,0,0,0,<..\..\rtgui\widgets\textbox.c><textbox.c>
+OPTFFF 11,186,1,0,0,0,0,0,<..\..\rtgui\widgets\title.c><title.c>
+OPTFFF 11,187,1,0,0,0,0,0,<..\..\rtgui\widgets\toplevel.c><toplevel.c>
+OPTFFF 11,188,1,0,0,0,0,0,<..\..\rtgui\server\mouse.c><mouse.c>
+OPTFFF 11,189,1,0,0,0,0,0,<..\..\rtgui\server\topwin.c><topwin.c>
+OPTFFF 11,190,1,0,0,0,0,0,<..\..\rtgui\common\font_hz_file.c><font_hz_file.c>
+OPTFFF 11,191,1,0,0,0,0,0,<..\..\rtgui\common\hz16font.c><hz16font.c>
+OPTFFF 11,192,1,0,0,0,0,0,<..\..\rtgui\common\hz12font.c><hz12font.c>
+OPTFFF 12,193,1,0,0,0,0,0,<.\Libraries\Mass_Storage\src\hw_config.c><hw_config.c>
+OPTFFF 12,194,1,0,0,0,0,0,<.\Libraries\Mass_Storage\src\mass_mal.c><mass_mal.c>
+OPTFFF 12,195,1,0,0,0,0,0,<.\Libraries\Mass_Storage\src\memory.c><memory.c>
+OPTFFF 12,196,1,0,0,0,0,0,<.\Libraries\Mass_Storage\src\usb_bot.c><usb_bot.c>
+OPTFFF 12,197,1,0,0,0,0,0,<.\Libraries\Mass_Storage\src\scsi_data.c><scsi_data.c>
+OPTFFF 12,198,1,0,0,0,0,0,<.\Libraries\Mass_Storage\src\usb_desc.c><usb_desc.c>
+OPTFFF 12,199,1,0,0,0,0,0,<.\Libraries\Mass_Storage\src\usb_endp.c><usb_endp.c>
+OPTFFF 12,200,1,0,0,0,0,0,<.\Libraries\Mass_Storage\src\usb_istr.c><usb_istr.c>
+OPTFFF 12,201,1,0,0,0,0,0,<.\Libraries\Mass_Storage\src\usb_prop.c><usb_prop.c>
+OPTFFF 12,202,1,0,0,0,0,0,<.\Libraries\Mass_Storage\src\usb_pwr.c><usb_pwr.c>
+OPTFFF 12,203,1,0,0,0,0,0,<.\Libraries\Mass_Storage\src\usb_scsi.c><usb_scsi.c>
+OPTFFF 12,204,1,0,0,0,0,0,<.\Libraries\Mass_Storage\src\usb_main.c><usb_main.c>
TARGOPT 1, (RT-Thread STM32 Radio)
@@ -256,13 +247,11 @@ TARGOPT 1, (RT-Thread STM32 Radio) OPTKEY 0,(ARMDBGFLAGS)(-T0)
OPTKEY 0,(DLGUARM)((105=150,189,819,540,0)(106=-1,-1,-1,-1,0)(107=-1,-1,-1,-1,0))
OPTKEY 0,(JL2CM3)(-U20090110 -O718 -S10 -C0 -JU1 -JI127.0.0.1 -JP0 -N00("ARM CoreSight SW-DP") -D00(1BA01477) -L00(0) -TO18 -TC10000000 -TP21 -TDS8004 -TDT0 -TDC1F -TIEFFFFFFFF -TIP8 -FO27 -FD20000000 -FC800 -FN1 -FF0STM32F10x_512 -FS08000000 -FL080000)
- OPTBB 0,0,41,1,134368182,0,0,0,0,1,<rtgui_system.c>()()
- OPTBB 1,0,44,1,134356542,0,0,0,0,1,<font.c>()()
OPTWA 0,1,(_mp)
OPTWA 1,1,(tinfo,0x0A)
OPTMM 1,8,(text)
OPTMM 2,8,(mimeBuffer)
- OPTDF 0x86
+ OPTDF 0x82
OPTLE <>
OPTLC <>
EndOpt
diff --git a/bsp/stm32_radio/project.Uv2 b/bsp/stm32_radio/project.Uv2 index 4ba21aae57..cdf0408321 100644 --- a/bsp/stm32_radio/project.Uv2 +++ b/bsp/stm32_radio/project.Uv2 @@ -119,18 +119,8 @@ File 8,1,<..\..\filesystem\dfs\src\dfs_fs.c><dfs_fs.c> File 8,1,<..\..\filesystem\dfs\src\dfs_raw.c><dfs_raw.c>
File 8,1,<..\..\filesystem\dfs\src\dfs_util.c><dfs_util.c>
File 8,1,<..\..\filesystem\dfs\src\dfs_posix.c><dfs_posix.c>
-File 8,1,<..\..\filesystem\dfs\filesystems\efsl\src\fs\vfat\dir.c><dir.c>
-File 8,1,<..\..\filesystem\dfs\filesystems\efsl\src\fs\vfat\fat.c><fat.c>
-File 8,1,<..\..\filesystem\dfs\filesystems\efsl\src\fs\vfat\file.c><file.c>
-File 8,1,<..\..\filesystem\dfs\filesystems\efsl\src\fs\vfat\fs.c><fs.c>
-File 8,1,<..\..\filesystem\dfs\filesystems\efsl\src\fs\vfat\ls.c><ls.c>
-File 8,1,<..\..\filesystem\dfs\filesystems\efsl\src\fs\vfat\time.c><time.c>
-File 8,1,<..\..\filesystem\dfs\filesystems\efsl\src\fs\vfat\ui.c><ui.c>
-File 8,1,<..\..\filesystem\dfs\filesystems\efsl\src\base\plibc.c><plibc.c>
-File 8,1,<..\..\filesystem\dfs\filesystems\efsl\src\base\efs.c><efs.c>
-File 8,1,<..\..\filesystem\dfs\filesystems\efsl\src\base\extract.c><extract.c>
-File 8,1,<..\..\filesystem\dfs\filesystems\efsl\src\base\partition.c><partition.c>
-File 8,1,<..\..\filesystem\dfs\src\dfs_cache.c><dfs_cache.c>
+File 8,1,<..\..\filesystem\dfs\filesystems\elmfat\dfs_elm.c><dfs_elm.c>
+File 8,1,<..\..\filesystem\dfs\filesystems\elmfat\ff.c><ff.c>
File 9,1,<..\..\net\lwip\src\core\dhcp.c><dhcp.c>
File 9,1,<..\..\net\lwip\src\core\dns.c><dns.c>
File 9,1,<..\..\net\lwip\src\core\init.c><init.c>
diff --git a/bsp/stm32_radio/rtconfig.h b/bsp/stm32_radio/rtconfig.h index c5536544b0..3830c790ef 100644 --- a/bsp/stm32_radio/rtconfig.h +++ b/bsp/stm32_radio/rtconfig.h @@ -1,161 +1,163 @@ -/* RT-Thread config file */ -#ifndef __RTTHREAD_CFG_H__ -#define __RTTHREAD_CFG_H__ - -/* RT_NAME_MAX*/ -#define RT_NAME_MAX 8 - -/* RT_ALIGN_SIZE*/ -#define RT_ALIGN_SIZE 4 - -/* PRIORITY_MAX*/ -#define RT_THREAD_PRIORITY_MAX 32 - -/* Tick per Second*/ -#define RT_TICK_PER_SECOND 100 - -/* SECTION: RT_DEBUG */ -/* Thread Debug*/ -#define RT_DEBUG -/* #define RT_THREAD_DEBUG */ - -#define RT_USING_OVERFLOW_CHECK - -/* Using Hook*/ -#define RT_USING_HOOK - -/* SECTION: IPC */ -/* Using Semaphore*/ -#define RT_USING_SEMAPHORE - -/* Using Mutex*/ -#define RT_USING_MUTEX - -/* Using Event*/ -#define RT_USING_EVENT - -/* Using MailBox*/ -#define RT_USING_MAILBOX - -/* Using Message Queue*/ -#define RT_USING_MESSAGEQUEUE - -/* SECTION: Memory Management */ -/* Using Memory Pool Management*/ -#define RT_USING_MEMPOOL - -/* Using Dynamic Heap Management*/ -#define RT_USING_HEAP - -/* Using Small MM*/ -#define RT_USING_SMALL_MEM - -/* Using SLAB Allocator*/ -/* #define RT_USING_SLAB */ - -/* SECTION: Device System */ -/* Using Device System*/ -#define RT_USING_DEVICE -#define RT_USING_UART1 -// #define RT_USING_UART2 -// #define RT_USING_UART3 - -/* SECTION: Console options */ -/* the buffer size of console*/ -#define RT_CONSOLEBUF_SIZE 128 - -/* SECTION: FinSH shell options */ -/* Using FinSH as Shell*/ -#define RT_USING_FINSH -/* Using symbol table */ -#define FINSH_USING_SYMTAB -#define FINSH_USING_DESCRIPTION -#define FINSH_DEVICE_NAME "uart1" - -/* SECTION: C++ support */ -/* Using C++ support*/ -/* #define RT_USING_CPLUSPLUS */ - -#define RT_USING_DFS -#define RT_USING_DFS_EFSL -/* byte alignment for EFSL */ -#define BYTE_ALIGNMENT -// #define RT_USING_DFS_ELMFAT -#define DFS_EFLS_USING_STATIC_CACHE -/* SECTION: DFS options */ -/* the max number of mounted filesystem */ -#define DFS_FILESYSTEMS_MAX 1 -/* the max number of opened files */ -#define DFS_FD_MAX 8 -/* the max number of cached sector */ -#define DFS_CACHE_MAX_NUM 4 - -/* SECTION: lwip, a lighwight TCP/IP protocol stack */ -/* Using lighweight TCP/IP protocol stack */ -#define RT_USING_LWIP - -/* Trace LwIP protocol */ -/* #define RT_LWIP_DEBUG */ - -/* LwIP uses RT-Thread Memory Management */ -#define RT_LWIP_USING_RT_MEM - -/* Enable ICMP protocol */ -#define RT_LWIP_ICMP - -/* Enable IGMP protocol */ -/* #define RT_LWIP_IGMP */ - -/* Enable UDP protocol */ -#define RT_LWIP_UDP - -/* Enable TCP protocol */ -#define RT_LWIP_TCP - -/* the number of simulatenously active TCP connections */ -#define RT_LWIP_TCP_PCB_NUM 3 - -/* TCP sender buffer space */ -#define RT_LWIP_TCP_SND_BUF 2048 - -/* Enable SNMP protocol */ -/* #define RT_LWIP_SNMP */ - -/* Using DHCP */ -#define RT_LWIP_DHCP - -/* Using DNS */ -#define RT_LWIP_DNS - -/* ip address of target */ -#define RT_LWIP_IPADDR0 192 -#define RT_LWIP_IPADDR1 168 -#define RT_LWIP_IPADDR2 1 -#define RT_LWIP_IPADDR3 30 - -/* gateway address of target */ -#define RT_LWIP_GWADDR0 192 -#define RT_LWIP_GWADDR1 168 -#define RT_LWIP_GWADDR2 1 -#define RT_LWIP_GWADDR3 1 - -/* mask address of target */ -#define RT_LWIP_MSKADDR0 255 -#define RT_LWIP_MSKADDR1 255 -#define RT_LWIP_MSKADDR2 255 -#define RT_LWIP_MSKADDR3 0 - -/* tcp thread options */ -#define RT_LWIP_TCPTHREAD_PRIORITY 10 -#define RT_LWIP_TCPTHREAD_MBOX_SIZE 4 -#define RT_LWIP_TCPTHREAD_STACKSIZE 1024 - -/* ethernet if thread options */ -#define RT_LWIP_ETHTHREAD_PRIORITY 12 -#define RT_LWIP_ETHTHREAD_MBOX_SIZE 4 -#define RT_LWIP_ETHTHREAD_STACKSIZE 512 - -/* SECTION: RT-Thread/GUI */ -#define RT_USING_RTGUI - -#endif +/* RT-Thread config file */
+#ifndef __RTTHREAD_CFG_H__
+#define __RTTHREAD_CFG_H__
+
+/* RT_NAME_MAX*/
+#define RT_NAME_MAX 8
+
+/* RT_ALIGN_SIZE*/
+#define RT_ALIGN_SIZE 4
+
+/* PRIORITY_MAX*/
+#define RT_THREAD_PRIORITY_MAX 32
+
+/* Tick per Second*/
+#define RT_TICK_PER_SECOND 100
+
+/* SECTION: RT_DEBUG */
+/* Thread Debug*/
+#define RT_DEBUG
+/* #define RT_THREAD_DEBUG */
+
+#define RT_USING_OVERFLOW_CHECK
+
+/* Using Hook*/
+#define RT_USING_HOOK
+
+/* SECTION: IPC */
+/* Using Semaphore*/
+#define RT_USING_SEMAPHORE
+
+/* Using Mutex*/
+#define RT_USING_MUTEX
+
+/* Using Event*/
+#define RT_USING_EVENT
+
+/* Using MailBox*/
+#define RT_USING_MAILBOX
+
+/* Using Message Queue*/
+#define RT_USING_MESSAGEQUEUE
+
+/* SECTION: Memory Management */
+/* Using Memory Pool Management*/
+#define RT_USING_MEMPOOL
+
+/* Using Dynamic Heap Management*/
+#define RT_USING_HEAP
+
+/* Using Small MM*/
+#define RT_USING_SMALL_MEM
+
+/* Using SLAB Allocator*/
+/* #define RT_USING_SLAB */
+
+/* SECTION: Device System */
+/* Using Device System*/
+#define RT_USING_DEVICE
+#define RT_USING_UART1
+// #define RT_USING_UART2
+// #define RT_USING_UART3
+
+/* SECTION: Console options */
+/* the buffer size of console*/
+#define RT_CONSOLEBUF_SIZE 128
+
+/* SECTION: FinSH shell options */
+/* Using FinSH as Shell*/
+#define RT_USING_FINSH
+/* Using symbol table */
+#define FINSH_USING_SYMTAB
+#define FINSH_USING_DESCRIPTION
+#define FINSH_DEVICE_NAME "uart1"
+
+/* SECTION: C++ support */
+/* Using C++ support*/
+/* #define RT_USING_CPLUSPLUS */
+
+#define RT_USING_DFS
+/* #define RT_USING_DFS_EFSL */
+/* byte alignment for EFSL */
+#define BYTE_ALIGNMENT
+
+#define RT_USING_DFS_ELMFAT
+#define RT_DFS_ELM_WORD_ACCESS
+
+/* SECTION: DFS options */
+/* the max number of mounted filesystem */
+#define DFS_FILESYSTEMS_MAX 2
+/* the max number of opened files */
+#define DFS_FD_MAX 8
+/* the max number of cached sector */
+#define DFS_CACHE_MAX_NUM 4
+
+/* SECTION: lwip, a lighwight TCP/IP protocol stack */
+/* Using lighweight TCP/IP protocol stack */
+#define RT_USING_LWIP
+
+/* Trace LwIP protocol */
+/* #define RT_LWIP_DEBUG */
+
+/* LwIP uses RT-Thread Memory Management */
+#define RT_LWIP_USING_RT_MEM
+
+/* Enable ICMP protocol */
+#define RT_LWIP_ICMP
+
+/* Enable IGMP protocol */
+/* #define RT_LWIP_IGMP */
+
+/* Enable UDP protocol */
+#define RT_LWIP_UDP
+
+/* Enable TCP protocol */
+#define RT_LWIP_TCP
+
+/* the number of simulatenously active TCP connections */
+#define RT_LWIP_TCP_PCB_NUM 3
+
+/* TCP sender buffer space */
+#define RT_LWIP_TCP_SND_BUF 2048
+
+/* Enable SNMP protocol */
+/* #define RT_LWIP_SNMP */
+
+/* Using DHCP */
+#define RT_LWIP_DHCP
+
+/* Using DNS */
+#define RT_LWIP_DNS
+
+/* ip address of target */
+#define RT_LWIP_IPADDR0 192
+#define RT_LWIP_IPADDR1 168
+#define RT_LWIP_IPADDR2 1
+#define RT_LWIP_IPADDR3 30
+
+/* gateway address of target */
+#define RT_LWIP_GWADDR0 192
+#define RT_LWIP_GWADDR1 168
+#define RT_LWIP_GWADDR2 1
+#define RT_LWIP_GWADDR3 1
+
+/* mask address of target */
+#define RT_LWIP_MSKADDR0 255
+#define RT_LWIP_MSKADDR1 255
+#define RT_LWIP_MSKADDR2 255
+#define RT_LWIP_MSKADDR3 0
+
+/* tcp thread options */
+#define RT_LWIP_TCPTHREAD_PRIORITY 10
+#define RT_LWIP_TCPTHREAD_MBOX_SIZE 4
+#define RT_LWIP_TCPTHREAD_STACKSIZE 1024
+
+/* ethernet if thread options */
+#define RT_LWIP_ETHTHREAD_PRIORITY 12
+#define RT_LWIP_ETHTHREAD_MBOX_SIZE 4
+#define RT_LWIP_ETHTHREAD_STACKSIZE 512
+
+/* SECTION: RT-Thread/GUI */
+#define RT_USING_RTGUI
+
+#endif
diff --git a/bsp/stm32_radio/sdcard.c b/bsp/stm32_radio/sdcard.c index 71592f2740..84f788312b 100644 --- a/bsp/stm32_radio/sdcard.c +++ b/bsp/stm32_radio/sdcard.c @@ -1,3153 +1,3163 @@ -/** - ****************************************************************************** - * @file SDIO/sdcard.c - * @author MCD Application Team - * @version V3.1.0 - * @date 06/19/2009 - * @brief This file provides all the SD Card driver firmware functions. - ****************************************************************************** - * @copy - * - * 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 2009 STMicroelectronics</center></h2> - */ - -/* Includes ------------------------------------------------------------------*/ -#include "sdcard.h" -#include <stm32f10x_dma.h> -#include <stm32f10x_sdio.h>
- +/**
+ ******************************************************************************
+ * @file SDIO/sdcard.c
+ * @author MCD Application Team
+ * @version V3.1.2
+ * @date 09/28/2009
+ * @brief This file provides all the SD Card driver firmware functions.
+ ******************************************************************************
+ * @copy
+ *
+ * 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 2009 STMicroelectronics</center></h2>
+ */
+
+/* Includes ------------------------------------------------------------------*/
+#include "sdcard.h"
#include <rtthread.h>
- -/** @addtogroup STM32F10x_StdPeriph_Examples - * @{ - */ - -/** @addtogroup SDIO_Example - * @{ - */ - -/* Private typedef -----------------------------------------------------------*/ -/* Private define ------------------------------------------------------------*/ -#define NULL 0 -#define SDIO_STATIC_FLAGS ((uint32_t)0x000005FF) -#define SDIO_CMD0TIMEOUT ((uint32_t)0x00002710) -#define SDIO_FIFO_Address ((uint32_t)0x40018080) - -/* 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) - -/* 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_VOLTAGE_WINDOW_MMC ((uint32_t)0x80FF8000) - -#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_CARD_PROGRAMMING ((uint32_t)0x00000007) -#define SD_CARD_RECEIVING ((uint32_t)0x00000006) -#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) - -/* 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) - -/* 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) - -#define SDIO_INIT_CLK_DIV ((uint8_t)0xB2) -#define SDIO_TRANSFER_CLK_DIV ((uint8_t)0x1) - -/* Private macro -------------------------------------------------------------*/ -/* 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 uint32_t DeviceMode = SD_POLLING_MODE; -static uint32_t TotalNumberOfBytes = 0, StopCondition = 0; -uint32_t *SrcBuffer, *DestBuffer; -volatile SD_Error TransferError = SD_OK; -__IO uint32_t TransferEnd = 0; -__IO uint32_t NumberOfBytes = 0; -SDIO_InitTypeDef SDIO_InitStructure; -SDIO_CmdInitTypeDef SDIO_CmdInitStructure; -SDIO_DataInitTypeDef SDIO_DataInitStructure; - -/* 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); -static uint8_t convert_from_bytes_to_power_of_two(uint16_t NumberOfBytes); -static void GPIO_Configuration(void); -static void DMA_TxConfiguration(uint32_t *BufferSRC, uint32_t BufferSize); -static void DMA_RxConfiguration(uint32_t *BufferDST, uint32_t BufferSize); - -/* Private functions ---------------------------------------------------------*/ - -/** - * @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; - - /* Configure SDIO interface GPIO */ - GPIO_Configuration(); - - /* Enable the SDIO AHB Clock */ - RCC_AHBPeriphClockCmd(RCC_AHBPeriph_SDIO, ENABLE); - - /* Enable the DMA2 Clock */ - RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA2, ENABLE); - - 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 */ - /* HCLK = 72 MHz, SDIOCLK = 72 MHz, SDIO_CK = HCLK/(2 + 1) = 24 MHz */ - 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 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; - bool validvoltage = FALSE; - uint32_t SDType = SD_STD_CAPACITY; - - /* Power ON Sequence -------------------------------------------------------*/ - /* Configure the SDIO peripheral */ - SDIO_InitStructure.SDIO_ClockDiv = SDIO_INIT_CLK_DIV; /* HCLK = 72MHz, SDIOCLK = 72MHz, SDIO_CK = HCLK/(178 + 2) = 400 KHz */ - 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 = SDIO_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 = SDIO_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(SDIO_APP_CMD); - } - /* CMD55 */ - SDIO_CmdInitStructure.SDIO_Argument = 0x00; - SDIO_CmdInitStructure.SDIO_CmdIndex = SDIO_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(SDIO_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 = SDIO_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(SDIO_APP_CMD); - - if (errorstatus != SD_OK) - { - return(errorstatus); - } - SDIO_CmdInitStructure.SDIO_Argument = SD_VOLTAGE_WINDOW_SD | SDType; - SDIO_CmdInitStructure.SDIO_CmdIndex = SDIO_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 = (bool) (((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 */ - else - { - CardType = SDIO_MULTIMEDIA_CARD; - - /* Send CMD1 SEND_OP_COND with Argument 0x80FF8000 */ - while ((!validvoltage) && (count < SD_MAX_VOLT_TRIAL)) - { - - /* SEND CMD55 APP_CMD with RCA as 0 */ - SDIO_CmdInitStructure.SDIO_Argument = SD_VOLTAGE_WINDOW_MMC; - SDIO_CmdInitStructure.SDIO_CmdIndex = SDIO_SEND_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 = (bool) (((response >> 31) == 1) ? 1 : 0); - count++; - } - if (count >= SD_MAX_VOLT_TRIAL) - { - errorstatus = SD_INVALID_VOLTRANGE; - return(errorstatus); - } - } - - return(SD_OK); -} - -/** - * @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 = SDIO_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 = SDIO_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(SDIO_SET_REL_ADDR, &rca); - - if (SD_OK != errorstatus) - { - return(errorstatus); - } - } - if (SDIO_MULTIMEDIA_CARD == CardType) - { - /* Send CMD3 SET_REL_ADDR with argument 0 */ - /* SD Card publishes its RCA. */ - SDIO_CmdInitStructure.SDIO_Argument = (uint32_t)(rca << 16); - SDIO_CmdInitStructure.SDIO_CmdIndex = SDIO_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 = CmdResp2Error(); - - 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 = SDIO_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_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 Sets device mode whether to operate in Polling, Interrupt or - * DMA mode. - * @param Mode: Specifies the Data Transfer mode. - * This parameter can be one of the following values: - * @arg SD_DMA_MODE: Data transfer using DMA. - * @arg SD_INTERRUPT_MODE: Data transfer using interrupts. - * @arg SD_POLLING_MODE: Data transfer using flags. - * @retval SD_Error: SD Card Error code. - */ -SD_Error SD_SetDeviceMode(uint32_t Mode) -{ - SD_Error errorstatus = SD_OK; - - if ((Mode == SD_DMA_MODE) || (Mode == SD_INTERRUPT_MODE) || (Mode == SD_POLLING_MODE)) - { - DeviceMode = Mode; - } - else - { - errorstatus = SD_INVALID_PARAMETER; - } - 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 = SDIO_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(SDIO_SEL_DESEL_CARD); - - return(errorstatus); -} - -/** - * @brief Allows to read one block from a specified address in a card. - * @param addr: Address from where data are to be read. - * @param readbuff: pointer to the buffer that will contain the - * received data - * @param BlockSize: the SD card Data block size. - * @retval SD_Error: SD Card Error code. - */ -SD_Error SD_ReadBlock(uint32_t addr, uint32_t *readbuff, uint16_t BlockSize) -{ - SD_Error errorstatus = SD_OK; - uint32_t count = 0, *tempbuff = readbuff; - uint8_t power = 0; - - if (NULL == readbuff) - { - errorstatus = SD_INVALID_PARAMETER; - return(errorstatus); - } - - TransferError = SD_OK; - TransferEnd = 0; - TotalNumberOfBytes = 0; - - /* Clear all DPSM configuration */ - SDIO_DataInitStructure.SDIO_DataTimeOut = SD_DATATIMEOUT; - SDIO_DataInitStructure.SDIO_DataLength = 0; - SDIO_DataInitStructure.SDIO_DataBlockSize = SDIO_DataBlockSize_1b; - SDIO_DataInitStructure.SDIO_TransferDir = SDIO_TransferDir_ToCard; - SDIO_DataInitStructure.SDIO_TransferMode = SDIO_TransferMode_Block; - SDIO_DataInitStructure.SDIO_DPSM = SDIO_DPSM_Disable; - SDIO_DataConfig(&SDIO_DataInitStructure); - SDIO_DMACmd(DISABLE); - - if (SDIO_GetResponse(SDIO_RESP1) & SD_CARD_LOCKED) - { - errorstatus = SD_LOCK_UNLOCK_FAILED; - return(errorstatus); - } - - if (CardType == SDIO_HIGH_CAPACITY_SD_CARD) - { - BlockSize = 512; - addr /= 512; - } - if ((BlockSize > 0) && (BlockSize <= 2048) && ((BlockSize & (BlockSize - 1)) == 0)) - { - power = convert_from_bytes_to_power_of_two(BlockSize); - - /* Set Block Size for Card */ - SDIO_CmdInitStructure.SDIO_Argument = (uint32_t) BlockSize; - SDIO_CmdInitStructure.SDIO_CmdIndex = SDIO_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(SDIO_SET_BLOCKLEN); - - if (SD_OK != errorstatus) - { - return(errorstatus); - } - } - else - { - errorstatus = SD_INVALID_PARAMETER; - return(errorstatus); - } - - SDIO_DataInitStructure.SDIO_DataTimeOut = SD_DATATIMEOUT; - SDIO_DataInitStructure.SDIO_DataLength = BlockSize; - SDIO_DataInitStructure.SDIO_DataBlockSize = (uint32_t) power << 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); - - TotalNumberOfBytes = BlockSize; - StopCondition = 0; - DestBuffer = readbuff; - - /* Send CMD17 READ_SINGLE_BLOCK */ - SDIO_CmdInitStructure.SDIO_Argument = (uint32_t)addr; - SDIO_CmdInitStructure.SDIO_CmdIndex = SDIO_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(SDIO_READ_SINGLE_BLOCK); - - if (errorstatus != SD_OK) - { - return(errorstatus); - } - /* In case of single block transfer, no need of stop transfer at all.*/ - if (DeviceMode == SD_POLLING_MODE) - { - /* 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); - } - else if (DeviceMode == SD_INTERRUPT_MODE) - { - SDIO_ITConfig(SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_DATAEND | SDIO_IT_RXOVERR | SDIO_IT_RXFIFOHF | SDIO_IT_STBITERR, ENABLE); - while ((TransferEnd == 0) && (TransferError == SD_OK)) - {} - if (TransferError != SD_OK) - { - return(TransferError); - } - } - else if (DeviceMode == SD_DMA_MODE) +
+/** @addtogroup STM32F10x_StdPeriph_Examples
+ * @{
+ */
+
+/** @addtogroup SDIO_Example
+ * @{
+ */
+
+/* Private typedef -----------------------------------------------------------*/
+/* Private define ------------------------------------------------------------*/
+#define NULL 0
+#define SDIO_STATIC_FLAGS ((uint32_t)0x000005FF)
+#define SDIO_CMD0TIMEOUT ((uint32_t)0x00002710)
+#define SDIO_FIFO_Address ((uint32_t)0x40018080)
+
+/* 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)
+
+/* 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_VOLTAGE_WINDOW_MMC ((uint32_t)0x80FF8000)
+
+#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_CARD_PROGRAMMING ((uint32_t)0x00000007)
+#define SD_CARD_RECEIVING ((uint32_t)0x00000006)
+#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)
+
+/* 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)
+
+/* 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)
+
+#define SDIO_INIT_CLK_DIV ((uint8_t)0xB2)
+#define SDIO_TRANSFER_CLK_DIV ((uint8_t)0x1)
+
+/* Private macro -------------------------------------------------------------*/
+/* 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 uint32_t DeviceMode = SD_POLLING_MODE;
+static uint32_t TotalNumberOfBytes = 0, StopCondition = 0;
+uint32_t *SrcBuffer, *DestBuffer;
+volatile SD_Error TransferError = SD_OK;
+__IO uint32_t TransferEnd = 0;
+__IO uint32_t NumberOfBytes = 0;
+SDIO_InitTypeDef SDIO_InitStructure;
+SDIO_CmdInitTypeDef SDIO_CmdInitStructure;
+SDIO_DataInitTypeDef SDIO_DataInitStructure;
+
+/* 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);
+static uint8_t convert_from_bytes_to_power_of_two(uint16_t NumberOfBytes);
+static void GPIO_Configuration(void);
+static void DMA_TxConfiguration(uint32_t *BufferSRC, uint32_t BufferSize);
+static void DMA_RxConfiguration(uint32_t *BufferDST, uint32_t BufferSize);
+
+/* Private functions ---------------------------------------------------------*/
+
+/**
+ * @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;
+
+ /* Configure SDIO interface GPIO */
+ GPIO_Configuration();
+
+ /* Enable the SDIO AHB Clock */
+ RCC_AHBPeriphClockCmd(RCC_AHBPeriph_SDIO, ENABLE);
+
+ /* Enable the DMA2 Clock */
+ RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA2, ENABLE);
+
+ SDIO_DeInit();
+
+ errorstatus = SD_PowerON();
+
+ if (errorstatus != SD_OK)
{
- int cnt = 0; - SDIO_ITConfig(SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_DATAEND | SDIO_IT_RXOVERR | SDIO_IT_STBITERR, ENABLE); - SDIO_DMACmd(ENABLE); - DMA_RxConfiguration(readbuff, BlockSize); - while (DMA_GetFlagStatus(DMA2_FLAG_TC4) == RESET) + /* 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 */
+ /* HCLK = 72 MHz, SDIOCLK = 72 MHz, SDIO_CK = HCLK/(2 + 1) = 24 MHz */
+ 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 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;
+ bool validvoltage = FALSE;
+ uint32_t SDType = SD_STD_CAPACITY;
+
+ /* Power ON Sequence -------------------------------------------------------*/
+ /* Configure the SDIO peripheral */
+ SDIO_InitStructure.SDIO_ClockDiv = SDIO_INIT_CLK_DIV; /* HCLK = 72MHz, SDIOCLK = 72MHz, SDIO_CK = HCLK/(178 + 2) = 400 KHz */
+ 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 = SDIO_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 = SDIO_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(SDIO_APP_CMD);
+ }
+ /* CMD55 */
+ SDIO_CmdInitStructure.SDIO_Argument = 0x00;
+ SDIO_CmdInitStructure.SDIO_CmdIndex = SDIO_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(SDIO_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))
{
- cnt ++;
- if (cnt > 10 * 50000)
- {
- rt_kprintf("DMA flag 0x%08x\n", DMA_GetFlagStatus(DMA2_FLAG_TC4));
- /* Clear all DPSM configuration */ - SDIO_DataInitStructure.SDIO_DataTimeOut = SD_DATATIMEOUT; - SDIO_DataInitStructure.SDIO_DataLength = 0; - SDIO_DataInitStructure.SDIO_DataBlockSize = SDIO_DataBlockSize_1b; - SDIO_DataInitStructure.SDIO_TransferDir = SDIO_TransferDir_ToCard; - SDIO_DataInitStructure.SDIO_TransferMode = SDIO_TransferMode_Block; - SDIO_DataInitStructure.SDIO_DPSM = SDIO_DPSM_Disable; - SDIO_DataConfig(&SDIO_DataInitStructure); - SDIO_DMACmd(DISABLE); - errorstatus = SD_ERROR;
- break;
- }
- }
- } - return(errorstatus); -} - -/** - * @brief Allows to read blocks from a specified address in a card. - * @param addr: Address from where data are to be read. - * @param readbuff: pointer to the buffer that will contain the - * received data. - * @param BlockSize: the SD card Data block size. - * @param NumberOfBlocks: number of blocks to be read. - * @retval SD_Error: SD Card Error code. - */ -SD_Error SD_ReadMultiBlocks(uint32_t addr, uint32_t *readbuff, uint16_t BlockSize, uint32_t NumberOfBlocks) -{ - SD_Error errorstatus = SD_OK; - uint32_t count = 0, *tempbuff = readbuff; - uint8_t power = 0; - - if (NULL == readbuff) - { - errorstatus = SD_INVALID_PARAMETER; - return(errorstatus); - } - - TransferError = SD_OK; - TransferEnd = 0; - TotalNumberOfBytes = 0; - - /* Clear all DPSM configuration */ - SDIO_DataInitStructure.SDIO_DataTimeOut = SD_DATATIMEOUT; - SDIO_DataInitStructure.SDIO_DataLength = 0; - SDIO_DataInitStructure.SDIO_DataBlockSize = SDIO_DataBlockSize_1b; - SDIO_DataInitStructure.SDIO_TransferDir = SDIO_TransferDir_ToCard; - SDIO_DataInitStructure.SDIO_TransferMode = SDIO_TransferMode_Block; - SDIO_DataInitStructure.SDIO_DPSM = SDIO_DPSM_Disable; - SDIO_DataConfig(&SDIO_DataInitStructure); - SDIO_DMACmd(DISABLE); - - if (SDIO_GetResponse(SDIO_RESP1) & SD_CARD_LOCKED) - { - errorstatus = SD_LOCK_UNLOCK_FAILED; - return(errorstatus); - } - - if (CardType == SDIO_HIGH_CAPACITY_SD_CARD) - { - BlockSize = 512; - addr /= 512; - } - - if ((BlockSize > 0) && (BlockSize <= 2048) && (0 == (BlockSize & (BlockSize - 1)))) - { - power = convert_from_bytes_to_power_of_two(BlockSize); - - /* Set Block Size for Card */ - SDIO_CmdInitStructure.SDIO_Argument = (uint32_t) BlockSize; - SDIO_CmdInitStructure.SDIO_CmdIndex = SDIO_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(SDIO_SET_BLOCKLEN); - - if (SD_OK != errorstatus) - { - return(errorstatus); - } - } - else - { - errorstatus = SD_INVALID_PARAMETER; - return(errorstatus); - } - - if (NumberOfBlocks > 1) - { - /* Common to all modes */ - if (NumberOfBlocks * BlockSize > SD_MAX_DATA_LENGTH) - { - errorstatus = SD_INVALID_PARAMETER; - return(errorstatus); - } - - TotalNumberOfBytes = NumberOfBlocks * BlockSize; - StopCondition = 1; - DestBuffer = readbuff; - - SDIO_DataInitStructure.SDIO_DataTimeOut = SD_DATATIMEOUT; - SDIO_DataInitStructure.SDIO_DataLength = NumberOfBlocks * BlockSize; - SDIO_DataInitStructure.SDIO_DataBlockSize = (uint32_t) power << 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)addr; - SDIO_CmdInitStructure.SDIO_CmdIndex = SDIO_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(SDIO_READ_MULT_BLOCK); - - if (errorstatus != SD_OK) - { - return(errorstatus); - } - - if (DeviceMode == SD_POLLING_MODE) - { - /* Polling mode */ - while (!(SDIO->STA &(SDIO_FLAG_RXOVERR | SDIO_FLAG_DCRCFAIL | SDIO_FLAG_DATAEND | SDIO_FLAG_DTIMEOUT | SDIO_FLAG_STBITERR))) - { - if (SDIO_GetFlagStatus(SDIO_FLAG_RXFIFOHF) != RESET) - { - for (count = 0; count < SD_HALFFIFO; count++) - { - *(tempbuff + count) = SDIO_ReadData(); - } - tempbuff += SD_HALFFIFO; - } - } - - 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++; - } - - if (SDIO_GetFlagStatus(SDIO_FLAG_DATAEND) != RESET) - { - /* In Case Of SD-CARD Send Command STOP_TRANSMISSION */ - if ((SDIO_STD_CAPACITY_SD_CARD_V1_1 == CardType) || (SDIO_HIGH_CAPACITY_SD_CARD == CardType) || (SDIO_STD_CAPACITY_SD_CARD_V2_0 == CardType)) - { - /* Send CMD12 STOP_TRANSMISSION */ - SDIO_CmdInitStructure.SDIO_Argument = 0x0; - SDIO_CmdInitStructure.SDIO_CmdIndex = SDIO_STOP_TRANSMISSION; - 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(SDIO_STOP_TRANSMISSION); - - if (errorstatus != SD_OK) - { - return(errorstatus); - } - } - } - /* Clear all the static flags */ - SDIO_ClearFlag(SDIO_STATIC_FLAGS); - } - else if (DeviceMode == SD_INTERRUPT_MODE) - { - SDIO_ITConfig(SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_DATAEND | SDIO_IT_RXOVERR | SDIO_IT_RXFIFOHF | SDIO_IT_STBITERR, ENABLE); - while ((TransferEnd == 0) && (TransferError == SD_OK)) - {} - if (TransferError != SD_OK) - { - return(TransferError); - } - } - else if (DeviceMode == SD_DMA_MODE) - { - SDIO_ITConfig(SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_DATAEND | SDIO_IT_RXOVERR | SDIO_IT_STBITERR, ENABLE); - SDIO_DMACmd(ENABLE); - DMA_RxConfiguration(readbuff, (NumberOfBlocks * BlockSize)); - while (DMA_GetFlagStatus(DMA2_FLAG_TC4) == RESET) - {} - while ((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. - * @param addr: 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. - * @retval SD_Error: SD Card Error code. - */ -SD_Error SD_WriteBlock(uint32_t addr, uint32_t *writebuff, uint16_t BlockSize) -{ - SD_Error errorstatus = SD_OK; - uint8_t power = 0, cardstate = 0; - uint32_t timeout = 0, bytestransferred = 0; - uint32_t cardstatus = 0, count = 0, restwords = 0; - uint32_t *tempbuff = writebuff; - - if (writebuff == NULL) - { - errorstatus = SD_INVALID_PARAMETER; - return(errorstatus); - } - - TransferError = SD_OK; - TransferEnd = 0; - TotalNumberOfBytes = 0; - - SDIO_DataInitStructure.SDIO_DataTimeOut = SD_DATATIMEOUT; - SDIO_DataInitStructure.SDIO_DataLength = 0; - SDIO_DataInitStructure.SDIO_DataBlockSize = SDIO_DataBlockSize_1b; - SDIO_DataInitStructure.SDIO_TransferDir = SDIO_TransferDir_ToCard; - SDIO_DataInitStructure.SDIO_TransferMode = SDIO_TransferMode_Block; - SDIO_DataInitStructure.SDIO_DPSM = SDIO_DPSM_Disable; - SDIO_DataConfig(&SDIO_DataInitStructure); - SDIO_DMACmd(DISABLE); - - if (SDIO_GetResponse(SDIO_RESP1) & SD_CARD_LOCKED) - { - errorstatus = SD_LOCK_UNLOCK_FAILED; - return(errorstatus); - } - - if (CardType == SDIO_HIGH_CAPACITY_SD_CARD) - { - BlockSize = 512; - addr /= 512; - } - - /* Set the block size, both on controller and card */ - if ((BlockSize > 0) && (BlockSize <= 2048) && ((BlockSize & (BlockSize - 1)) == 0)) - { - power = convert_from_bytes_to_power_of_two(BlockSize); - - SDIO_CmdInitStructure.SDIO_Argument = (uint32_t) BlockSize; - SDIO_CmdInitStructure.SDIO_CmdIndex = SDIO_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(SDIO_SET_BLOCKLEN); - - if (errorstatus != SD_OK) - { - return(errorstatus); - } - } - else - { - errorstatus = SD_INVALID_PARAMETER; - return(errorstatus); - } - - /* Wait till card is ready for data Added */ - SDIO_CmdInitStructure.SDIO_Argument = (uint32_t) (RCA << 16); - SDIO_CmdInitStructure.SDIO_CmdIndex = SDIO_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); - - errorstatus = CmdResp1Error(SDIO_SEND_STATUS); - - if (errorstatus != SD_OK) - { - return(errorstatus); - } - - cardstatus = SDIO_GetResponse(SDIO_RESP1); - - timeout = SD_DATATIMEOUT; - - while (((cardstatus & 0x00000100) == 0) && (timeout > 0)) - { - timeout--; - SDIO_CmdInitStructure.SDIO_Argument = (uint32_t) (RCA << 16); - SDIO_CmdInitStructure.SDIO_CmdIndex = SDIO_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); - - errorstatus = CmdResp1Error(SDIO_SEND_STATUS); - - if (errorstatus != SD_OK) - { - return(errorstatus); - } - cardstatus = SDIO_GetResponse(SDIO_RESP1); - } - - if (timeout == 0) - { - return(SD_ERROR); - } - - /* Send CMD24 WRITE_SINGLE_BLOCK */ - SDIO_CmdInitStructure.SDIO_Argument = addr; - SDIO_CmdInitStructure.SDIO_CmdIndex = SDIO_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(SDIO_WRITE_SINGLE_BLOCK); - - if (errorstatus != SD_OK) - { - return(errorstatus); - } - - TotalNumberOfBytes = BlockSize; - StopCondition = 0; - SrcBuffer = writebuff; - - SDIO_DataInitStructure.SDIO_DataTimeOut = SD_DATATIMEOUT; - SDIO_DataInitStructure.SDIO_DataLength = BlockSize; - SDIO_DataInitStructure.SDIO_DataBlockSize = (uint32_t) power << 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 (DeviceMode == 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 ((TotalNumberOfBytes - bytestransferred) < 32) - { - restwords = ((TotalNumberOfBytes - bytestransferred) % 4 == 0) ? ((TotalNumberOfBytes - bytestransferred) / 4) : (( TotalNumberOfBytes - 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); - } - } - else if (DeviceMode == SD_INTERRUPT_MODE) - { - SDIO_ITConfig(SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_DATAEND | SDIO_FLAG_TXFIFOHE | SDIO_IT_TXUNDERR | SDIO_IT_STBITERR, ENABLE); - while ((TransferEnd == 0) && (TransferError == SD_OK)) - {} - if (TransferError != SD_OK) - { - return(TransferError); - } - } - else if (DeviceMode == SD_DMA_MODE) - { - SDIO_ITConfig(SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_DATAEND | SDIO_IT_TXUNDERR | SDIO_IT_STBITERR, ENABLE); - DMA_TxConfiguration(writebuff, BlockSize); - SDIO_DMACmd(ENABLE); - while (DMA_GetFlagStatus(DMA2_FLAG_TC4) == RESET) - {} - while ((TransferEnd == 0) && (TransferError == SD_OK)) - {} - if (TransferError != SD_OK) - { - return(TransferError); - } - } - - /* Clear all the static flags */ - SDIO_ClearFlag(SDIO_STATIC_FLAGS); - - /* Wait till the card is in programming state */ - errorstatus = IsCardProgramming(&cardstate); - - while ((errorstatus == SD_OK) && ((cardstate == SD_CARD_PROGRAMMING) || (cardstate == SD_CARD_RECEIVING))) - { - errorstatus = IsCardProgramming(&cardstate); - } - - return(errorstatus); -} - -/** - * @brief Allows to write blocks starting from a specified address in - * a card. - * @param addr: 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. - * @param NumberOfBlocks: number of blocks to be written. - * @retval SD_Error: SD Card Error code. - */ -SD_Error SD_WriteMultiBlocks(uint32_t addr, uint32_t *writebuff, uint16_t BlockSize, uint32_t NumberOfBlocks) -{ - SD_Error errorstatus = SD_OK; - uint8_t power = 0, cardstate = 0; - uint32_t bytestransferred = 0; - uint32_t count = 0, restwords = 0; - uint32_t *tempbuff = writebuff; - - if (writebuff == NULL) - { - errorstatus = SD_INVALID_PARAMETER; - return(errorstatus); - } - - TransferError = SD_OK; - TransferEnd = 0; - TotalNumberOfBytes = 0; - - SDIO_DataInitStructure.SDIO_DataTimeOut = SD_DATATIMEOUT; - SDIO_DataInitStructure.SDIO_DataLength = 0; - SDIO_DataInitStructure.SDIO_DataBlockSize = SDIO_DataBlockSize_1b; - SDIO_DataInitStructure.SDIO_TransferDir = SDIO_TransferDir_ToCard; - SDIO_DataInitStructure.SDIO_TransferMode = SDIO_TransferMode_Block; - SDIO_DataInitStructure.SDIO_DPSM = SDIO_DPSM_Disable; - SDIO_DataConfig(&SDIO_DataInitStructure); - SDIO_DMACmd(DISABLE); - - if (SDIO_GetResponse(SDIO_RESP1) & SD_CARD_LOCKED) - { - errorstatus = SD_LOCK_UNLOCK_FAILED; - return(errorstatus); - } - - if (CardType == SDIO_HIGH_CAPACITY_SD_CARD) - { - BlockSize = 512; - addr /= 512; - } - - /* Set the block size, both on controller and card */ - if ((BlockSize > 0) && (BlockSize <= 2048) && ((BlockSize & (BlockSize - 1)) == 0)) - { - power = convert_from_bytes_to_power_of_two(BlockSize); - - SDIO_CmdInitStructure.SDIO_Argument = (uint32_t) BlockSize; - SDIO_CmdInitStructure.SDIO_CmdIndex = SDIO_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(SDIO_SET_BLOCKLEN); - - if (errorstatus != SD_OK) - { - return(errorstatus); - } - } - else - { - errorstatus = SD_INVALID_PARAMETER; - return(errorstatus); - } - - /* Wait till card is ready for data Added */ - SDIO_CmdInitStructure.SDIO_Argument = (uint32_t) (RCA << 16); - SDIO_CmdInitStructure.SDIO_CmdIndex = SDIO_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); - - errorstatus = CmdResp1Error(SDIO_SEND_STATUS); - - if (errorstatus != SD_OK) - { - return(errorstatus); - } - - if (NumberOfBlocks > 1) - { - /* Common to all modes */ - if (NumberOfBlocks * BlockSize > SD_MAX_DATA_LENGTH) - { - errorstatus = SD_INVALID_PARAMETER; - return(errorstatus); - } - - if ((SDIO_STD_CAPACITY_SD_CARD_V1_1 == CardType) || (SDIO_STD_CAPACITY_SD_CARD_V2_0 == CardType) || (SDIO_HIGH_CAPACITY_SD_CARD == CardType)) - { - /* To improve performance */ - SDIO_CmdInitStructure.SDIO_Argument = (uint32_t) (RCA << 16); - SDIO_CmdInitStructure.SDIO_CmdIndex = SDIO_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(SDIO_APP_CMD); - - if (errorstatus != SD_OK) - { - return(errorstatus); - } - /* To improve performance */ - SDIO_CmdInitStructure.SDIO_Argument = (uint32_t)NumberOfBlocks; - SDIO_CmdInitStructure.SDIO_CmdIndex = SDIO_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(SDIO_SET_BLOCK_COUNT); - - if (errorstatus != SD_OK) - { - return(errorstatus); - } - } - - /* Send CMD25 WRITE_MULT_BLOCK with argument data address */ - SDIO_CmdInitStructure.SDIO_Argument = (uint32_t)addr; - SDIO_CmdInitStructure.SDIO_CmdIndex = SDIO_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(SDIO_WRITE_MULT_BLOCK); - - if (SD_OK != errorstatus) - { - return(errorstatus); - } - - TotalNumberOfBytes = NumberOfBlocks * BlockSize; - StopCondition = 1; - SrcBuffer = writebuff; - - SDIO_DataInitStructure.SDIO_DataTimeOut = SD_DATATIMEOUT; - SDIO_DataInitStructure.SDIO_DataLength = NumberOfBlocks * BlockSize; - SDIO_DataInitStructure.SDIO_DataBlockSize = (uint32_t) power << 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); - - if (DeviceMode == SD_POLLING_MODE) - { - while (!(SDIO->STA & (SDIO_FLAG_TXUNDERR | SDIO_FLAG_DCRCFAIL | SDIO_FLAG_DATAEND | SDIO_FLAG_DTIMEOUT | SDIO_FLAG_STBITERR))) - { - if (SDIO_GetFlagStatus(SDIO_FLAG_TXFIFOHE) != RESET) - { - if (!((TotalNumberOfBytes - bytestransferred) < SD_HALFFIFOBYTES)) - { - for (count = 0; count < SD_HALFFIFO; count++) - { - SDIO_WriteData(*(tempbuff + count)); - } - tempbuff += SD_HALFFIFO; - bytestransferred += SD_HALFFIFOBYTES; - } - else - { - restwords = ((TotalNumberOfBytes - bytestransferred) % 4 == 0) ? ((TotalNumberOfBytes - bytestransferred) / 4) : - ((TotalNumberOfBytes - bytestransferred) / 4 + 1); - - for (count = 0; count < restwords; count++, tempbuff++, bytestransferred += 4) - { - SDIO_WriteData(*tempbuff); - } - } - } - } - - 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); - } - - if (SDIO_GetFlagStatus(SDIO_FLAG_DATAEND) != RESET) - { - if ((SDIO_STD_CAPACITY_SD_CARD_V1_1 == CardType) || (SDIO_STD_CAPACITY_SD_CARD_V2_0 == CardType) || (SDIO_HIGH_CAPACITY_SD_CARD == CardType)) - { - /* Send CMD12 STOP_TRANSMISSION */ - SDIO_CmdInitStructure.SDIO_Argument = 0x0; - SDIO_CmdInitStructure.SDIO_CmdIndex = SDIO_STOP_TRANSMISSION; - 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(SDIO_STOP_TRANSMISSION); - - if (errorstatus != SD_OK) - { - return(errorstatus); - } - } - } - } - else if (DeviceMode == SD_INTERRUPT_MODE) - { - SDIO_ITConfig(SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_DATAEND | SDIO_IT_TXFIFOHE | SDIO_IT_TXUNDERR | SDIO_IT_STBITERR, ENABLE); - while ((TransferEnd == 0) && (TransferError == SD_OK)) - {} - if (TransferError != SD_OK) - { - return(TransferError); - } - } - else if (DeviceMode == SD_DMA_MODE) - { - SDIO_ITConfig(SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_DATAEND | SDIO_IT_TXUNDERR | SDIO_IT_STBITERR, ENABLE); - SDIO_DMACmd(ENABLE); - DMA_TxConfiguration(writebuff, (NumberOfBlocks * BlockSize)); - while (DMA_GetFlagStatus(DMA2_FLAG_TC4) == RESET) - {} - while ((TransferEnd == 0) && (TransferError == SD_OK)) - {} - if (TransferError != SD_OK) - { - return(TransferError); - } - } - } - /* Clear all the static flags */ - SDIO_ClearFlag(SDIO_STATIC_FLAGS); - - /* Wait till the card is in programming state */ - errorstatus = IsCardProgramming(&cardstate); - - while ((errorstatus == SD_OK) && ((cardstate == SD_CARD_PROGRAMMING) || (cardstate == SD_CARD_RECEIVING))) - { - errorstatus = IsCardProgramming(&cardstate); - } - - return(errorstatus); -} - -/** - * @brief Gets the cuurent data transfer state. - * @param None - * @retval SDTransferState: Data Transfer state. - * This value can be: - * - SD_NO_TRANSFER: No data transfer is acting - * - SD_TRANSFER_IN_PROGRESS: Data transfer is acting - */ -SDTransferState SD_GetTransferState(void) -{ - if (SDIO->STA & (SDIO_FLAG_TXACT | SDIO_FLAG_RXACT)) - { - return(SD_TRANSFER_IN_PROGRESS); - } - else - { - return(SD_NO_TRANSFER); - } -} - -/** - * @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_CmdInitStructure.SDIO_Argument = 0x0; - SDIO_CmdInitStructure.SDIO_CmdIndex = SDIO_STOP_TRANSMISSION; - 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(SDIO_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 = 72000 / ((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 = SDIO_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(SDIO_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 = SDIO_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(SDIO_SD_ERASE_GRP_END); - if (errorstatus != SD_OK) - { - return(errorstatus); - } - } - - /* Send CMD38 ERASE */ - SDIO_CmdInitStructure.SDIO_Argument = 0; - SDIO_CmdInitStructure.SDIO_CmdIndex = SDIO_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(SDIO_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; - - if (pcardstatus == NULL) - { - errorstatus = SD_INVALID_PARAMETER; - return(errorstatus); - } - - SDIO_CmdInitStructure.SDIO_Argument = (uint32_t) RCA << 16; - SDIO_CmdInitStructure.SDIO_CmdIndex = SDIO_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); - - - errorstatus = CmdResp1Error(SDIO_SEND_STATUS); - - if (errorstatus != SD_OK) - { - return(errorstatus); - } - - *pcardstatus = SDIO_GetResponse(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 = SDIO_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(SDIO_SET_BLOCKLEN); - - if (errorstatus != SD_OK) - { - return(errorstatus); - } - - /* CMD55 */ - SDIO_CmdInitStructure.SDIO_Argument = (uint32_t) RCA << 16; - SDIO_CmdInitStructure.SDIO_CmdIndex = SDIO_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(SDIO_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 = SDIO_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(SDIO_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); - psdstatus -= 16; - for (count = 0; count < 16; count++) - { - psdstatus[count] = ((psdstatus[count] & SD_0TO7BITS) << 24) |((psdstatus[count] & SD_8TO15BITS) << 8) | - ((psdstatus[count] & SD_16TO23BITS) >> 8) |((psdstatus[count] & SD_24TO31BITS) >> 24); - } - 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) -{ - uint32_t count = 0, restwords = 0; - - if (DeviceMode == SD_INTERRUPT_MODE) - { - if (SDIO_GetITStatus(SDIO_IT_RXFIFOHF) != RESET) - { - for (count = 0; count < SD_HALFFIFO; count++) - { - *(DestBuffer + count) = SDIO_ReadData(); - } - DestBuffer += SD_HALFFIFO; - NumberOfBytes += SD_HALFFIFOBYTES; - } - else if (SDIO_GetITStatus(SDIO_IT_TXFIFOHE) != RESET) - { - if ((TotalNumberOfBytes - NumberOfBytes) < SD_HALFFIFOBYTES) - { - restwords = ((TotalNumberOfBytes - NumberOfBytes) % 4 == 0) ? - ((TotalNumberOfBytes - NumberOfBytes) / 4) : - ((TotalNumberOfBytes - NumberOfBytes) / 4 + 1); - - for (count = 0; count < restwords; count++, SrcBuffer++, NumberOfBytes += 4) - { - SDIO_WriteData(*SrcBuffer); - } - } - else - { - for (count = 0; count < SD_HALFFIFO; count++) - { - SDIO_WriteData(*(SrcBuffer + count)); - } - - SrcBuffer += SD_HALFFIFO; - NumberOfBytes += SD_HALFFIFOBYTES; - } - } - } - - if (SDIO_GetITStatus(SDIO_IT_DATAEND) != RESET) - { - if (DeviceMode != SD_DMA_MODE) - { - while ((SDIO_GetFlagStatus(SDIO_FLAG_RXDAVL) != RESET) && (NumberOfBytes < TotalNumberOfBytes)) - { - *DestBuffer = SDIO_ReadData(); - DestBuffer++; - NumberOfBytes += 4; - } - } - - if (StopCondition == 1) - { - TransferError = SD_StopTransfer(); - } - else - { - TransferError = SD_OK; - } - SDIO_ClearITPendingBit(SDIO_IT_DATAEND); - SDIO_ITConfig(SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_DATAEND | - SDIO_IT_TXFIFOHE | SDIO_IT_RXFIFOHF | SDIO_IT_TXUNDERR | - SDIO_IT_RXOVERR | SDIO_IT_STBITERR, DISABLE); - TransferEnd = 1; - NumberOfBytes = 0; - return(TransferError); - } - - if (SDIO_GetITStatus(SDIO_IT_DCRCFAIL) != RESET) - { - SDIO_ClearITPendingBit(SDIO_IT_DCRCFAIL); - SDIO_ITConfig(SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_DATAEND | - SDIO_IT_TXFIFOHE | SDIO_IT_RXFIFOHF | SDIO_IT_TXUNDERR | - SDIO_IT_RXOVERR | SDIO_IT_STBITERR, DISABLE); - NumberOfBytes = 0; - TransferError = SD_DATA_CRC_FAIL; - return(SD_DATA_CRC_FAIL); - } - - if (SDIO_GetITStatus(SDIO_IT_DTIMEOUT) != RESET) - { - SDIO_ClearITPendingBit(SDIO_IT_DTIMEOUT); - SDIO_ITConfig(SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_DATAEND | - SDIO_IT_TXFIFOHE | SDIO_IT_RXFIFOHF | SDIO_IT_TXUNDERR | - SDIO_IT_RXOVERR | SDIO_IT_STBITERR, DISABLE); - NumberOfBytes = 0; - TransferError = SD_DATA_TIMEOUT; - return(SD_DATA_TIMEOUT); - } - - if (SDIO_GetITStatus(SDIO_IT_RXOVERR) != RESET) - { - SDIO_ClearITPendingBit(SDIO_IT_RXOVERR); - SDIO_ITConfig(SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_DATAEND | - SDIO_IT_TXFIFOHE | SDIO_IT_RXFIFOHF | SDIO_IT_TXUNDERR | - SDIO_IT_RXOVERR | SDIO_IT_STBITERR, DISABLE); - NumberOfBytes = 0; - TransferError = SD_RX_OVERRUN; - return(SD_RX_OVERRUN); - } - - if (SDIO_GetITStatus(SDIO_IT_TXUNDERR) != RESET) - { - SDIO_ClearITPendingBit(SDIO_IT_TXUNDERR); - SDIO_ITConfig(SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_DATAEND | - SDIO_IT_TXFIFOHE | SDIO_IT_RXFIFOHF | SDIO_IT_TXUNDERR | - SDIO_IT_RXOVERR | SDIO_IT_STBITERR, DISABLE); - NumberOfBytes = 0; - TransferError = SD_TX_UNDERRUN; - return(SD_TX_UNDERRUN); - } - - if (SDIO_GetITStatus(SDIO_IT_STBITERR) != RESET) - { - SDIO_ClearITPendingBit(SDIO_IT_STBITERR); - SDIO_ITConfig(SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_DATAEND | - SDIO_IT_TXFIFOHE | SDIO_IT_RXFIFOHF | SDIO_IT_TXUNDERR | - SDIO_IT_RXOVERR | SDIO_IT_STBITERR, DISABLE); - NumberOfBytes = 0; - TransferError = SD_START_BIT_ERR; - return(SD_START_BIT_ERR); - } - - return(SD_OK); -} - -/** - * @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) -{ - SD_Error errorstatus = SD_OK; - uint32_t status; - uint32_t response_r1; - - 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); - } - - /* 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 for analysis */ - response_r1 = SDIO_GetResponse(SDIO_RESP1); - - if ((response_r1 & SD_OCR_ERRORBITS) == SD_ALLZERO) - { - return(errorstatus); - } - - if (response_r1 & SD_OCR_ADDR_OUT_OF_RANGE) - { - return(SD_ADDR_OUT_OF_RANGE); - } - - if (response_r1 & SD_OCR_ADDR_MISALIGNED) - { - return(SD_ADDR_MISALIGNED); - } - - if (response_r1 & SD_OCR_BLOCK_LEN_ERR) - { - return(SD_BLOCK_LEN_ERR); - } - - if (response_r1 & SD_OCR_ERASE_SEQ_ERR) - { - return(SD_ERASE_SEQ_ERR); - } - - if (response_r1 & SD_OCR_BAD_ERASE_PARAM) - { - return(SD_BAD_ERASE_PARAM); - } - - if (response_r1 & SD_OCR_WRITE_PROT_VIOLATION) - { - return(SD_WRITE_PROT_VIOLATION); - } - - if (response_r1 & SD_OCR_LOCK_UNLOCK_FAILED) - { - return(SD_LOCK_UNLOCK_FAILED); - } - - if (response_r1 & SD_OCR_COM_CRC_FAILED) - { - return(SD_COM_CRC_FAILED); - } - - if (response_r1 & SD_OCR_ILLEGAL_CMD) - { - return(SD_ILLEGAL_CMD); - } - - if (response_r1 & SD_OCR_CARD_ECC_FAILED) - { - return(SD_CARD_ECC_FAILED); - } - - if (response_r1 & SD_OCR_CC_ERROR) - { - return(SD_CC_ERROR); - } - - if (response_r1 & SD_OCR_GENERAL_UNKNOWN_ERROR) - { - return(SD_GENERAL_UNKNOWN_ERROR); - } - - if (response_r1 & SD_OCR_STREAM_READ_UNDERRUN) - { - return(SD_STREAM_READ_UNDERRUN); - } - - if (response_r1 & SD_OCR_STREAM_WRITE_OVERRUN) - { - return(SD_STREAM_WRITE_OVERRUN); - } - - if (response_r1 & SD_OCR_CID_CSD_OVERWRIETE) - { - return(SD_CID_CSD_OVERWRITE); - } - - if (response_r1 & SD_OCR_WP_ERASE_SKIP) - { - return(SD_WP_ERASE_SKIP); - } - - if (response_r1 & SD_OCR_CARD_ECC_DISABLED) - { - return(SD_CARD_ECC_DISABLED); - } - - if (response_r1 & SD_OCR_ERASE_RESET) - { - return(SD_ERASE_RESET); - } - - if (response_r1 & SD_OCR_AKE_SEQ_ERROR) - { - return(SD_AKE_SEQ_ERROR); - } - return(errorstatus); -} - -/** - * @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 = SDIO_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(SDIO_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 = SDIO_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(SDIO_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 = SDIO_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(SDIO_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 = SDIO_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(SDIO_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 = SDIO_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 != SDIO_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 = SDIO_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(SDIO_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 = SDIO_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(SDIO_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 = SDIO_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(SDIO_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 - */ -static uint8_t convert_from_bytes_to_power_of_two(uint16_t NumberOfBytes) -{ - uint8_t count = 0; - - while (NumberOfBytes != 1) - { - NumberOfBytes >>= 1; - count++; - } - return(count); -} - -/** - * @brief Configures the SDIO Corresponding GPIO Ports - * @param None - * @retval None - */ -static void GPIO_Configuration(void) -{ - GPIO_InitTypeDef GPIO_InitStructure; - - /* GPIOC and GPIOD Periph clock enable */ - RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOD, ENABLE); - - /* Configure PC.08, PC.09, PC.10, PC.11, PC.12 pin: D0, D1, D2, D3, CLK pin */ - 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); - - /* Configure PD.02 CMD line */ - GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; - GPIO_Init(GPIOD, &GPIO_InitStructure); -} - -/** - * @brief Configures the DMA2 Channel4 for SDIO Tx request. - * @param BufferSRC: pointer to the source buffer - * @param BufferSize: buffer size - * @retval None - */ -static void DMA_TxConfiguration(uint32_t *BufferSRC, uint32_t BufferSize) -{ - DMA_InitTypeDef DMA_InitStructure; - - DMA_ClearFlag(DMA2_FLAG_TC4 | DMA2_FLAG_TE4 | DMA2_FLAG_HT4 | DMA2_FLAG_GL4); - - /* DMA2 Channel4 disable */ - DMA_Cmd(DMA2_Channel4, DISABLE); - - /* DMA2 Channel4 Config */ - 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); - - /* DMA2 Channel4 enable */ - DMA_Cmd(DMA2_Channel4, ENABLE); -} - -/** - * @brief Configures the DMA2 Channel4 for SDIO Rx request. - * @param BufferDST: pointer to the destination buffer - * @param BufferSize: buffer size - * @retval None - */ -static void DMA_RxConfiguration(uint32_t *BufferDST, uint32_t BufferSize) -{ - DMA_InitTypeDef DMA_InitStructure; - - DMA_ClearFlag(DMA2_FLAG_TC4 | DMA2_FLAG_TE4 | DMA2_FLAG_HT4 | DMA2_FLAG_GL4); - - /* DMA2 Channel4 disable */ - DMA_Cmd(DMA2_Channel4, DISABLE); - - /* DMA2 Channel4 Config */ - 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_VeryHigh; - DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; +
+ /* SEND CMD55 APP_CMD with RCA as 0 */
+ SDIO_CmdInitStructure.SDIO_Argument = 0x00;
+ SDIO_CmdInitStructure.SDIO_CmdIndex = SDIO_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(SDIO_APP_CMD);
+
+ if (errorstatus != SD_OK)
+ {
+ return(errorstatus);
+ }
+ SDIO_CmdInitStructure.SDIO_Argument = SD_VOLTAGE_WINDOW_SD | SDType;
+ SDIO_CmdInitStructure.SDIO_CmdIndex = SDIO_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 = (bool) (((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 */
+ else
+ {
+ CardType = SDIO_MULTIMEDIA_CARD;
+
+ /* Send CMD1 SEND_OP_COND with Argument 0x80FF8000 */
+ while ((!validvoltage) && (count < SD_MAX_VOLT_TRIAL))
+ {
+ /* SEND CMD55 APP_CMD with RCA as 0 */
+ SDIO_CmdInitStructure.SDIO_Argument = SD_VOLTAGE_WINDOW_MMC;
+ SDIO_CmdInitStructure.SDIO_CmdIndex = SDIO_SEND_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 = (bool) (((response >> 31) == 1) ? 1 : 0);
+ count++;
+ }
+ if (count >= SD_MAX_VOLT_TRIAL)
+ {
+ errorstatus = SD_INVALID_VOLTRANGE;
+ return(errorstatus);
+ }
+ }
+
+ return(SD_OK);
+}
+
+/**
+ * @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 = SDIO_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 = SDIO_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(SDIO_SET_REL_ADDR, &rca);
+
+ if (SD_OK != errorstatus)
+ {
+ return(errorstatus);
+ }
+ }
+ if (SDIO_MULTIMEDIA_CARD == CardType)
+ {
+ /* Send CMD3 SET_REL_ADDR with argument 0 */
+ /* SD Card publishes its RCA. */
+ SDIO_CmdInitStructure.SDIO_Argument = (uint32_t)(rca << 16);
+ SDIO_CmdInitStructure.SDIO_CmdIndex = SDIO_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 = CmdResp2Error();
+
+ 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 = SDIO_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_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 Sets device mode whether to operate in Polling, Interrupt or
+ * DMA mode.
+ * @param Mode: Specifies the Data Transfer mode.
+ * This parameter can be one of the following values:
+ * @arg SD_DMA_MODE: Data transfer using DMA.
+ * @arg SD_INTERRUPT_MODE: Data transfer using interrupts.
+ * @arg SD_POLLING_MODE: Data transfer using flags.
+ * @retval SD_Error: SD Card Error code.
+ */
+SD_Error SD_SetDeviceMode(uint32_t Mode)
+{
+ SD_Error errorstatus = SD_OK;
+
+ if ((Mode == SD_DMA_MODE) || (Mode == SD_INTERRUPT_MODE) || (Mode == SD_POLLING_MODE))
+ {
+ DeviceMode = Mode;
+ }
+ else
+ {
+ errorstatus = SD_INVALID_PARAMETER;
+ }
+ 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 = SDIO_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(SDIO_SEL_DESEL_CARD);
+
+ return(errorstatus);
+}
+
+/**
+ * @brief Allows to read one block from a specified address in a card.
+ * @param addr: Address from where data are to be read.
+ * @param readbuff: pointer to the buffer that will contain the
+ * received data
+ * @param BlockSize: the SD card Data block size.
+ * @retval SD_Error: SD Card Error code.
+ */
+SD_Error SD_ReadBlock(uint32_t addr, uint32_t *readbuff, uint16_t BlockSize)
+{
+ SD_Error errorstatus = SD_OK;
+ uint32_t count = 0, *tempbuff = readbuff;
+ uint8_t power = 0;
+
+ if (NULL == readbuff)
+ {
+ errorstatus = SD_INVALID_PARAMETER;
+ return(errorstatus);
+ }
+
+ TransferError = SD_OK;
+ TransferEnd = 0;
+ TotalNumberOfBytes = 0;
+
+ /* Clear all DPSM configuration */
+ SDIO_DataInitStructure.SDIO_DataTimeOut = SD_DATATIMEOUT;
+ SDIO_DataInitStructure.SDIO_DataLength = 0;
+ SDIO_DataInitStructure.SDIO_DataBlockSize = SDIO_DataBlockSize_1b;
+ SDIO_DataInitStructure.SDIO_TransferDir = SDIO_TransferDir_ToCard;
+ SDIO_DataInitStructure.SDIO_TransferMode = SDIO_TransferMode_Block;
+ SDIO_DataInitStructure.SDIO_DPSM = SDIO_DPSM_Disable;
+ SDIO_DataConfig(&SDIO_DataInitStructure);
+ SDIO_DMACmd(DISABLE);
+
+ if (SDIO_GetResponse(SDIO_RESP1) & SD_CARD_LOCKED)
+ {
+ errorstatus = SD_LOCK_UNLOCK_FAILED;
+ return(errorstatus);
+ }
+
+ if (CardType == SDIO_HIGH_CAPACITY_SD_CARD)
+ {
+ BlockSize = 512;
+ addr /= 512;
+ }
+ if ((BlockSize > 0) && (BlockSize <= 2048) && ((BlockSize & (BlockSize - 1)) == 0))
+ {
+ power = convert_from_bytes_to_power_of_two(BlockSize);
+
+ /* Set Block Size for Card */
+ SDIO_CmdInitStructure.SDIO_Argument = (uint32_t) BlockSize;
+ SDIO_CmdInitStructure.SDIO_CmdIndex = SDIO_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(SDIO_SET_BLOCKLEN);
+
+ if (SD_OK != errorstatus)
+ {
+ return(errorstatus);
+ }
+ }
+ else
+ {
+ errorstatus = SD_INVALID_PARAMETER;
+ return(errorstatus);
+ }
+
+ SDIO_DataInitStructure.SDIO_DataTimeOut = SD_DATATIMEOUT;
+ SDIO_DataInitStructure.SDIO_DataLength = BlockSize;
+ SDIO_DataInitStructure.SDIO_DataBlockSize = (uint32_t) power << 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);
+
+ TotalNumberOfBytes = BlockSize;
+ StopCondition = 0;
+ DestBuffer = readbuff;
+
+ /* Send CMD17 READ_SINGLE_BLOCK */
+ SDIO_CmdInitStructure.SDIO_Argument = (uint32_t)addr;
+ SDIO_CmdInitStructure.SDIO_CmdIndex = SDIO_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(SDIO_READ_SINGLE_BLOCK);
+
+ if (errorstatus != SD_OK)
+ {
+ return(errorstatus);
+ }
+ /* In case of single block transfer, no need of stop transfer at all.*/
+ if (DeviceMode == SD_POLLING_MODE)
+ {
+ /* 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);
+ }
+ else if (DeviceMode == SD_INTERRUPT_MODE)
+ {
+ SDIO_ITConfig(SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_DATAEND | SDIO_IT_RXOVERR | SDIO_IT_RXFIFOHF | SDIO_IT_STBITERR, ENABLE);
+ while ((TransferEnd == 0) && (TransferError == SD_OK))
+ {}
+ if (TransferError != SD_OK)
+ {
+ return(TransferError);
+ }
+ }
+ else if (DeviceMode == SD_DMA_MODE)
+ {
+ SDIO_ITConfig(SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_DATAEND | SDIO_IT_RXOVERR | SDIO_IT_STBITERR, ENABLE);
+ SDIO_DMACmd(ENABLE);
+ DMA_RxConfiguration(readbuff, BlockSize);
+ while (DMA_GetFlagStatus(DMA2_FLAG_TC4) == RESET)
+ {}
+ }
+ return(errorstatus);
+}
+
+/**
+ * @brief Allows to read blocks from a specified address in a card.
+ * @param addr: Address from where data are to be read.
+ * @param readbuff: pointer to the buffer that will contain the
+ * received data.
+ * @param BlockSize: the SD card Data block size.
+ * @param NumberOfBlocks: number of blocks to be read.
+ * @retval SD_Error: SD Card Error code.
+ */
+SD_Error SD_ReadMultiBlocks(uint32_t addr, uint32_t *readbuff, uint16_t BlockSize, uint32_t NumberOfBlocks)
+{
+ SD_Error errorstatus = SD_OK;
+ uint32_t count = 0, *tempbuff = readbuff;
+ uint8_t power = 0;
+
+ if (NULL == readbuff)
+ {
+ errorstatus = SD_INVALID_PARAMETER;
+ return(errorstatus);
+ }
+
+ TransferError = SD_OK;
+ TransferEnd = 0;
+ TotalNumberOfBytes = 0;
+
+ /* Clear all DPSM configuration */
+ SDIO_DataInitStructure.SDIO_DataTimeOut = SD_DATATIMEOUT;
+ SDIO_DataInitStructure.SDIO_DataLength = 0;
+ SDIO_DataInitStructure.SDIO_DataBlockSize = SDIO_DataBlockSize_1b;
+ SDIO_DataInitStructure.SDIO_TransferDir = SDIO_TransferDir_ToCard;
+ SDIO_DataInitStructure.SDIO_TransferMode = SDIO_TransferMode_Block;
+ SDIO_DataInitStructure.SDIO_DPSM = SDIO_DPSM_Disable;
+ SDIO_DataConfig(&SDIO_DataInitStructure);
+ SDIO_DMACmd(DISABLE);
+
+ if (SDIO_GetResponse(SDIO_RESP1) & SD_CARD_LOCKED)
+ {
+ errorstatus = SD_LOCK_UNLOCK_FAILED;
+ return(errorstatus);
+ }
+
+ if (CardType == SDIO_HIGH_CAPACITY_SD_CARD)
+ {
+ BlockSize = 512;
+ addr /= 512;
+ }
+
+ if ((BlockSize > 0) && (BlockSize <= 2048) && (0 == (BlockSize & (BlockSize - 1))))
+ {
+ power = convert_from_bytes_to_power_of_two(BlockSize);
+
+ /* Set Block Size for Card */
+ SDIO_CmdInitStructure.SDIO_Argument = (uint32_t) BlockSize;
+ SDIO_CmdInitStructure.SDIO_CmdIndex = SDIO_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(SDIO_SET_BLOCKLEN);
+
+ if (SD_OK != errorstatus)
+ {
+ return(errorstatus);
+ }
+ }
+ else
+ {
+ errorstatus = SD_INVALID_PARAMETER;
+ return(errorstatus);
+ }
+
+ if (NumberOfBlocks > 1)
+ {
+ /* Common to all modes */
+ if (NumberOfBlocks * BlockSize > SD_MAX_DATA_LENGTH)
+ {
+ errorstatus = SD_INVALID_PARAMETER;
+ return(errorstatus);
+ }
+
+ TotalNumberOfBytes = NumberOfBlocks * BlockSize;
+ StopCondition = 1;
+ DestBuffer = readbuff;
+
+ SDIO_DataInitStructure.SDIO_DataTimeOut = SD_DATATIMEOUT;
+ SDIO_DataInitStructure.SDIO_DataLength = NumberOfBlocks * BlockSize;
+ SDIO_DataInitStructure.SDIO_DataBlockSize = (uint32_t) power << 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)addr;
+ SDIO_CmdInitStructure.SDIO_CmdIndex = SDIO_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(SDIO_READ_MULT_BLOCK);
+
+ if (errorstatus != SD_OK)
+ {
+ return(errorstatus);
+ }
+
+ if (DeviceMode == SD_POLLING_MODE)
+ {
+ /* Polling mode */
+ while (!(SDIO->STA &(SDIO_FLAG_RXOVERR | SDIO_FLAG_DCRCFAIL | SDIO_FLAG_DATAEND | SDIO_FLAG_DTIMEOUT | SDIO_FLAG_STBITERR)))
+ {
+ if (SDIO_GetFlagStatus(SDIO_FLAG_RXFIFOHF) != RESET)
+ {
+ for (count = 0; count < SD_HALFFIFO; count++)
+ {
+ *(tempbuff + count) = SDIO_ReadData();
+ }
+ tempbuff += SD_HALFFIFO;
+ }
+ }
+
+ 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++;
+ }
+
+ if (SDIO_GetFlagStatus(SDIO_FLAG_DATAEND) != RESET)
+ {
+ /* In Case Of SD-CARD Send Command STOP_TRANSMISSION */
+ if ((SDIO_STD_CAPACITY_SD_CARD_V1_1 == CardType) || (SDIO_HIGH_CAPACITY_SD_CARD == CardType) || (SDIO_STD_CAPACITY_SD_CARD_V2_0 == CardType))
+ {
+ /* Send CMD12 STOP_TRANSMISSION */
+ SDIO_CmdInitStructure.SDIO_Argument = 0x0;
+ SDIO_CmdInitStructure.SDIO_CmdIndex = SDIO_STOP_TRANSMISSION;
+ 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(SDIO_STOP_TRANSMISSION);
+
+ if (errorstatus != SD_OK)
+ {
+ return(errorstatus);
+ }
+ }
+ }
+ /* Clear all the static flags */
+ SDIO_ClearFlag(SDIO_STATIC_FLAGS);
+ }
+ else if (DeviceMode == SD_INTERRUPT_MODE)
+ {
+ SDIO_ITConfig(SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_DATAEND | SDIO_IT_RXOVERR | SDIO_IT_RXFIFOHF | SDIO_IT_STBITERR, ENABLE);
+ while ((TransferEnd == 0) && (TransferError == SD_OK))
+ {}
+ if (TransferError != SD_OK)
+ {
+ return(TransferError);
+ }
+ }
+ else if (DeviceMode == SD_DMA_MODE)
+ {
+ SDIO_ITConfig(SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_DATAEND | SDIO_IT_RXOVERR | SDIO_IT_STBITERR, ENABLE);
+ SDIO_DMACmd(ENABLE);
+ DMA_RxConfiguration(readbuff, (NumberOfBlocks * BlockSize));
+ while (DMA_GetFlagStatus(DMA2_FLAG_TC4) == RESET)
+ {}
+ while ((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.
+ * @param addr: 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.
+ * @retval SD_Error: SD Card Error code.
+ */
+SD_Error SD_WriteBlock(uint32_t addr, uint32_t *writebuff, uint16_t BlockSize)
+{
+ SD_Error errorstatus = SD_OK;
+ uint8_t power = 0, cardstate = 0;
+ uint32_t timeout = 0, bytestransferred = 0;
+ uint32_t cardstatus = 0, count = 0, restwords = 0;
+ uint32_t *tempbuff = writebuff;
+
+ if (writebuff == NULL)
+ {
+ errorstatus = SD_INVALID_PARAMETER;
+ return(errorstatus);
+ }
+
+ TransferError = SD_OK;
+ TransferEnd = 0;
+ TotalNumberOfBytes = 0;
+
+ SDIO_DataInitStructure.SDIO_DataTimeOut = SD_DATATIMEOUT;
+ SDIO_DataInitStructure.SDIO_DataLength = 0;
+ SDIO_DataInitStructure.SDIO_DataBlockSize = SDIO_DataBlockSize_1b;
+ SDIO_DataInitStructure.SDIO_TransferDir = SDIO_TransferDir_ToCard;
+ SDIO_DataInitStructure.SDIO_TransferMode = SDIO_TransferMode_Block;
+ SDIO_DataInitStructure.SDIO_DPSM = SDIO_DPSM_Disable;
+ SDIO_DataConfig(&SDIO_DataInitStructure);
+ SDIO_DMACmd(DISABLE);
+
+ if (SDIO_GetResponse(SDIO_RESP1) & SD_CARD_LOCKED)
+ {
+ errorstatus = SD_LOCK_UNLOCK_FAILED;
+ return(errorstatus);
+ }
+
+ if (CardType == SDIO_HIGH_CAPACITY_SD_CARD)
+ {
+ BlockSize = 512;
+ addr /= 512;
+ }
+
+ /* Set the block size, both on controller and card */
+ if ((BlockSize > 0) && (BlockSize <= 2048) && ((BlockSize & (BlockSize - 1)) == 0))
+ {
+ power = convert_from_bytes_to_power_of_two(BlockSize);
+
+ SDIO_CmdInitStructure.SDIO_Argument = (uint32_t) BlockSize;
+ SDIO_CmdInitStructure.SDIO_CmdIndex = SDIO_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(SDIO_SET_BLOCKLEN);
+
+ if (errorstatus != SD_OK)
+ {
+ return(errorstatus);
+ }
+ }
+ else
+ {
+ errorstatus = SD_INVALID_PARAMETER;
+ return(errorstatus);
+ }
+
+ /* Wait till card is ready for data Added */
+ SDIO_CmdInitStructure.SDIO_Argument = (uint32_t) (RCA << 16);
+ SDIO_CmdInitStructure.SDIO_CmdIndex = SDIO_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);
+
+ errorstatus = CmdResp1Error(SDIO_SEND_STATUS);
+
+ if (errorstatus != SD_OK)
+ {
+ return(errorstatus);
+ }
+
+ cardstatus = SDIO_GetResponse(SDIO_RESP1);
+
+ timeout = SD_DATATIMEOUT;
+
+ while (((cardstatus & 0x00000100) == 0) && (timeout > 0))
+ {
+ timeout--;
+ SDIO_CmdInitStructure.SDIO_Argument = (uint32_t) (RCA << 16);
+ SDIO_CmdInitStructure.SDIO_CmdIndex = SDIO_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);
+
+ errorstatus = CmdResp1Error(SDIO_SEND_STATUS);
+
+ if (errorstatus != SD_OK)
+ {
+ return(errorstatus);
+ }
+ cardstatus = SDIO_GetResponse(SDIO_RESP1);
+ }
+
+ if (timeout == 0)
+ {
+ return(SD_ERROR);
+ }
+
+ /* Send CMD24 WRITE_SINGLE_BLOCK */
+ SDIO_CmdInitStructure.SDIO_Argument = addr;
+ SDIO_CmdInitStructure.SDIO_CmdIndex = SDIO_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(SDIO_WRITE_SINGLE_BLOCK);
+
+ if (errorstatus != SD_OK)
+ {
+ return(errorstatus);
+ }
+
+ TotalNumberOfBytes = BlockSize;
+ StopCondition = 0;
+ SrcBuffer = writebuff;
+
+ SDIO_DataInitStructure.SDIO_DataTimeOut = SD_DATATIMEOUT;
+ SDIO_DataInitStructure.SDIO_DataLength = BlockSize;
+ SDIO_DataInitStructure.SDIO_DataBlockSize = (uint32_t) power << 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 (DeviceMode == 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 ((TotalNumberOfBytes - bytestransferred) < 32)
+ {
+ restwords = ((TotalNumberOfBytes - bytestransferred) % 4 == 0) ? ((TotalNumberOfBytes - bytestransferred) / 4) : (( TotalNumberOfBytes - 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);
+ }
+ }
+ else if (DeviceMode == SD_INTERRUPT_MODE)
+ {
+ SDIO_ITConfig(SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_DATAEND | SDIO_FLAG_TXFIFOHE | SDIO_IT_TXUNDERR | SDIO_IT_STBITERR, ENABLE);
+ while ((TransferEnd == 0) && (TransferError == SD_OK))
+ {}
+ if (TransferError != SD_OK)
+ {
+ return(TransferError);
+ }
+ }
+ else if (DeviceMode == SD_DMA_MODE)
+ {
+ SDIO_ITConfig(SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_DATAEND | SDIO_IT_TXUNDERR | SDIO_IT_STBITERR, ENABLE);
+ DMA_TxConfiguration(writebuff, BlockSize);
+ SDIO_DMACmd(ENABLE);
+ while (DMA_GetFlagStatus(DMA2_FLAG_TC4) == RESET)
+ {}
+ while ((TransferEnd == 0) && (TransferError == SD_OK))
+ {}
+ if (TransferError != SD_OK)
+ {
+ return(TransferError);
+ }
+ }
+
+ /* Clear all the static flags */
+ SDIO_ClearFlag(SDIO_STATIC_FLAGS);
+
+ /* Wait till the card is in programming state */
+ errorstatus = IsCardProgramming(&cardstate);
+
+ while ((errorstatus == SD_OK) && ((cardstate == SD_CARD_PROGRAMMING) || (cardstate == SD_CARD_RECEIVING)))
+ {
+ errorstatus = IsCardProgramming(&cardstate);
+ }
+
+ return(errorstatus);
+}
+
+/**
+ * @brief Allows to write blocks starting from a specified address in
+ * a card.
+ * @param addr: 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.
+ * @param NumberOfBlocks: number of blocks to be written.
+ * @retval SD_Error: SD Card Error code.
+ */
+SD_Error SD_WriteMultiBlocks(uint32_t addr, uint32_t *writebuff, uint16_t BlockSize, uint32_t NumberOfBlocks)
+{
+ SD_Error errorstatus = SD_OK;
+ uint8_t power = 0, cardstate = 0;
+ uint32_t bytestransferred = 0;
+ uint32_t count = 0, restwords = 0;
+ uint32_t *tempbuff = writebuff;
+
+ if (writebuff == NULL)
+ {
+ errorstatus = SD_INVALID_PARAMETER;
+ return(errorstatus);
+ }
+
+ TransferError = SD_OK;
+ TransferEnd = 0;
+ TotalNumberOfBytes = 0;
+
+ SDIO_DataInitStructure.SDIO_DataTimeOut = SD_DATATIMEOUT;
+ SDIO_DataInitStructure.SDIO_DataLength = 0;
+ SDIO_DataInitStructure.SDIO_DataBlockSize = SDIO_DataBlockSize_1b;
+ SDIO_DataInitStructure.SDIO_TransferDir = SDIO_TransferDir_ToCard;
+ SDIO_DataInitStructure.SDIO_TransferMode = SDIO_TransferMode_Block;
+ SDIO_DataInitStructure.SDIO_DPSM = SDIO_DPSM_Disable;
+ SDIO_DataConfig(&SDIO_DataInitStructure);
+ SDIO_DMACmd(DISABLE);
+
+ if (SDIO_GetResponse(SDIO_RESP1) & SD_CARD_LOCKED)
+ {
+ errorstatus = SD_LOCK_UNLOCK_FAILED;
+ return(errorstatus);
+ }
+
+ if (CardType == SDIO_HIGH_CAPACITY_SD_CARD)
+ {
+ BlockSize = 512;
+ addr /= 512;
+ }
+
+ /* Set the block size, both on controller and card */
+ if ((BlockSize > 0) && (BlockSize <= 2048) && ((BlockSize & (BlockSize - 1)) == 0))
+ {
+ power = convert_from_bytes_to_power_of_two(BlockSize);
+
+ SDIO_CmdInitStructure.SDIO_Argument = (uint32_t) BlockSize;
+ SDIO_CmdInitStructure.SDIO_CmdIndex = SDIO_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(SDIO_SET_BLOCKLEN);
+
+ if (errorstatus != SD_OK)
+ {
+ return(errorstatus);
+ }
+ }
+ else
+ {
+ errorstatus = SD_INVALID_PARAMETER;
+ return(errorstatus);
+ }
+
+ /* Wait till card is ready for data Added */
+ SDIO_CmdInitStructure.SDIO_Argument = (uint32_t) (RCA << 16);
+ SDIO_CmdInitStructure.SDIO_CmdIndex = SDIO_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);
+
+ errorstatus = CmdResp1Error(SDIO_SEND_STATUS);
+
+ if (errorstatus != SD_OK)
+ {
+ return(errorstatus);
+ }
+
+ if (NumberOfBlocks > 1)
+ {
+ /* Common to all modes */
+ if (NumberOfBlocks * BlockSize > SD_MAX_DATA_LENGTH)
+ {
+ errorstatus = SD_INVALID_PARAMETER;
+ return(errorstatus);
+ }
+
+ if ((SDIO_STD_CAPACITY_SD_CARD_V1_1 == CardType) || (SDIO_STD_CAPACITY_SD_CARD_V2_0 == CardType) || (SDIO_HIGH_CAPACITY_SD_CARD == CardType))
+ {
+ /* To improve performance */
+ SDIO_CmdInitStructure.SDIO_Argument = (uint32_t) (RCA << 16);
+ SDIO_CmdInitStructure.SDIO_CmdIndex = SDIO_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(SDIO_APP_CMD);
+
+ if (errorstatus != SD_OK)
+ {
+ return(errorstatus);
+ }
+ /* To improve performance */
+ SDIO_CmdInitStructure.SDIO_Argument = (uint32_t)NumberOfBlocks;
+ SDIO_CmdInitStructure.SDIO_CmdIndex = SDIO_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(SDIO_SET_BLOCK_COUNT);
+
+ if (errorstatus != SD_OK)
+ {
+ return(errorstatus);
+ }
+ }
+
+ /* Send CMD25 WRITE_MULT_BLOCK with argument data address */
+ SDIO_CmdInitStructure.SDIO_Argument = (uint32_t)addr;
+ SDIO_CmdInitStructure.SDIO_CmdIndex = SDIO_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(SDIO_WRITE_MULT_BLOCK);
+
+ if (SD_OK != errorstatus)
+ {
+ return(errorstatus);
+ }
+
+ TotalNumberOfBytes = NumberOfBlocks * BlockSize;
+ StopCondition = 1;
+ SrcBuffer = writebuff;
+
+ SDIO_DataInitStructure.SDIO_DataTimeOut = SD_DATATIMEOUT;
+ SDIO_DataInitStructure.SDIO_DataLength = NumberOfBlocks * BlockSize;
+ SDIO_DataInitStructure.SDIO_DataBlockSize = (uint32_t) power << 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);
+
+ if (DeviceMode == SD_POLLING_MODE)
+ {
+ while (!(SDIO->STA & (SDIO_FLAG_TXUNDERR | SDIO_FLAG_DCRCFAIL | SDIO_FLAG_DATAEND | SDIO_FLAG_DTIMEOUT | SDIO_FLAG_STBITERR)))
+ {
+ if (SDIO_GetFlagStatus(SDIO_FLAG_TXFIFOHE) != RESET)
+ {
+ if (!((TotalNumberOfBytes - bytestransferred) < SD_HALFFIFOBYTES))
+ {
+ for (count = 0; count < SD_HALFFIFO; count++)
+ {
+ SDIO_WriteData(*(tempbuff + count));
+ }
+ tempbuff += SD_HALFFIFO;
+ bytestransferred += SD_HALFFIFOBYTES;
+ }
+ else
+ {
+ restwords = ((TotalNumberOfBytes - bytestransferred) % 4 == 0) ? ((TotalNumberOfBytes - bytestransferred) / 4) :
+ ((TotalNumberOfBytes - bytestransferred) / 4 + 1);
+
+ for (count = 0; count < restwords; count++, tempbuff++, bytestransferred += 4)
+ {
+ SDIO_WriteData(*tempbuff);
+ }
+ }
+ }
+ }
+
+ 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);
+ }
+
+ if (SDIO_GetFlagStatus(SDIO_FLAG_DATAEND) != RESET)
+ {
+ if ((SDIO_STD_CAPACITY_SD_CARD_V1_1 == CardType) || (SDIO_STD_CAPACITY_SD_CARD_V2_0 == CardType) || (SDIO_HIGH_CAPACITY_SD_CARD == CardType))
+ {
+ /* Send CMD12 STOP_TRANSMISSION */
+ SDIO_CmdInitStructure.SDIO_Argument = 0x0;
+ SDIO_CmdInitStructure.SDIO_CmdIndex = SDIO_STOP_TRANSMISSION;
+ 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(SDIO_STOP_TRANSMISSION);
+
+ if (errorstatus != SD_OK)
+ {
+ return(errorstatus);
+ }
+ }
+ }
+ }
+ else if (DeviceMode == SD_INTERRUPT_MODE)
+ {
+ SDIO_ITConfig(SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_DATAEND | SDIO_IT_TXFIFOHE | SDIO_IT_TXUNDERR | SDIO_IT_STBITERR, ENABLE);
+ while ((TransferEnd == 0) && (TransferError == SD_OK))
+ {}
+ if (TransferError != SD_OK)
+ {
+ return(TransferError);
+ }
+ }
+ else if (DeviceMode == SD_DMA_MODE)
+ {
+ SDIO_ITConfig(SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_DATAEND | SDIO_IT_TXUNDERR | SDIO_IT_STBITERR, ENABLE);
+ SDIO_DMACmd(ENABLE);
+ DMA_TxConfiguration(writebuff, (NumberOfBlocks * BlockSize));
+ while (DMA_GetFlagStatus(DMA2_FLAG_TC4) == RESET)
+ {}
+ while ((TransferEnd == 0) && (TransferError == SD_OK))
+ {}
+ if (TransferError != SD_OK)
+ {
+ return(TransferError);
+ }
+ }
+ }
+ /* Clear all the static flags */
+ SDIO_ClearFlag(SDIO_STATIC_FLAGS);
+
+ /* Wait till the card is in programming state */
+ errorstatus = IsCardProgramming(&cardstate);
+
+ while ((errorstatus == SD_OK) && ((cardstate == SD_CARD_PROGRAMMING) || (cardstate == SD_CARD_RECEIVING)))
+ {
+ errorstatus = IsCardProgramming(&cardstate);
+ }
+
+ return(errorstatus);
+}
+
+/**
+ * @brief Gets the cuurent data transfer state.
+ * @param None
+ * @retval SDTransferState: Data Transfer state.
+ * This value can be:
+ * - SD_NO_TRANSFER: No data transfer is acting
+ * - SD_TRANSFER_IN_PROGRESS: Data transfer is acting
+ */
+SDTransferState SD_GetTransferState(void)
+{
+ if (SDIO->STA & (SDIO_FLAG_TXACT | SDIO_FLAG_RXACT))
+ {
+ return(SD_TRANSFER_IN_PROGRESS);
+ }
+ else
+ {
+ return(SD_NO_TRANSFER);
+ }
+}
+
+/**
+ * @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_CmdInitStructure.SDIO_Argument = 0x0;
+ SDIO_CmdInitStructure.SDIO_CmdIndex = SDIO_STOP_TRANSMISSION;
+ 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(SDIO_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 = 72000 / ((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 = SDIO_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(SDIO_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 = SDIO_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(SDIO_SD_ERASE_GRP_END);
+ if (errorstatus != SD_OK)
+ {
+ return(errorstatus);
+ }
+ }
+
+ /* Send CMD38 ERASE */
+ SDIO_CmdInitStructure.SDIO_Argument = 0;
+ SDIO_CmdInitStructure.SDIO_CmdIndex = SDIO_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(SDIO_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;
+
+ if (pcardstatus == NULL)
+ {
+ errorstatus = SD_INVALID_PARAMETER;
+ return(errorstatus);
+ }
+
+ SDIO_CmdInitStructure.SDIO_Argument = (uint32_t) RCA << 16;
+ SDIO_CmdInitStructure.SDIO_CmdIndex = SDIO_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);
+
+
+ errorstatus = CmdResp1Error(SDIO_SEND_STATUS);
+
+ if (errorstatus != SD_OK)
+ {
+ return(errorstatus);
+ }
+
+ *pcardstatus = SDIO_GetResponse(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 = SDIO_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(SDIO_SET_BLOCKLEN);
+
+ if (errorstatus != SD_OK)
+ {
+ return(errorstatus);
+ }
+
+ /* CMD55 */
+ SDIO_CmdInitStructure.SDIO_Argument = (uint32_t) RCA << 16;
+ SDIO_CmdInitStructure.SDIO_CmdIndex = SDIO_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(SDIO_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 = SDIO_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(SDIO_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);
+ psdstatus -= 16;
+ for (count = 0; count < 16; count++)
+ {
+ psdstatus[count] = ((psdstatus[count] & SD_0TO7BITS) << 24) |((psdstatus[count] & SD_8TO15BITS) << 8) |
+ ((psdstatus[count] & SD_16TO23BITS) >> 8) |((psdstatus[count] & SD_24TO31BITS) >> 24);
+ }
+ 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)
+{
+ uint32_t count = 0, restwords = 0;
+
+ if (DeviceMode == SD_INTERRUPT_MODE)
+ {
+ if (SDIO_GetITStatus(SDIO_IT_RXFIFOHF) != RESET)
+ {
+ for (count = 0; count < SD_HALFFIFO; count++)
+ {
+ *(DestBuffer + count) = SDIO_ReadData();
+ }
+ DestBuffer += SD_HALFFIFO;
+ NumberOfBytes += SD_HALFFIFOBYTES;
+ }
+ else if (SDIO_GetITStatus(SDIO_IT_TXFIFOHE) != RESET)
+ {
+ if ((TotalNumberOfBytes - NumberOfBytes) < SD_HALFFIFOBYTES)
+ {
+ restwords = ((TotalNumberOfBytes - NumberOfBytes) % 4 == 0) ?
+ ((TotalNumberOfBytes - NumberOfBytes) / 4) :
+ ((TotalNumberOfBytes - NumberOfBytes) / 4 + 1);
+
+ for (count = 0; count < restwords; count++, SrcBuffer++, NumberOfBytes += 4)
+ {
+ SDIO_WriteData(*SrcBuffer);
+ }
+ }
+ else
+ {
+ for (count = 0; count < SD_HALFFIFO; count++)
+ {
+ SDIO_WriteData(*(SrcBuffer + count));
+ }
+
+ SrcBuffer += SD_HALFFIFO;
+ NumberOfBytes += SD_HALFFIFOBYTES;
+ }
+ }
+ }
+
+ if (SDIO_GetITStatus(SDIO_IT_DATAEND) != RESET)
+ {
+ if (DeviceMode != SD_DMA_MODE)
+ {
+ while ((SDIO_GetFlagStatus(SDIO_FLAG_RXDAVL) != RESET) && (NumberOfBytes < TotalNumberOfBytes))
+ {
+ *DestBuffer = SDIO_ReadData();
+ DestBuffer++;
+ NumberOfBytes += 4;
+ }
+ }
+
+ if (StopCondition == 1)
+ {
+ TransferError = SD_StopTransfer();
+ }
+ else
+ {
+ TransferError = SD_OK;
+ }
+ SDIO_ClearITPendingBit(SDIO_IT_DATAEND);
+ SDIO_ITConfig(SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_DATAEND |
+ SDIO_IT_TXFIFOHE | SDIO_IT_RXFIFOHF | SDIO_IT_TXUNDERR |
+ SDIO_IT_RXOVERR | SDIO_IT_STBITERR, DISABLE);
+ TransferEnd = 1;
+ NumberOfBytes = 0;
+ return(TransferError);
+ }
+
+ if (SDIO_GetITStatus(SDIO_IT_DCRCFAIL) != RESET)
+ {
+ SDIO_ClearITPendingBit(SDIO_IT_DCRCFAIL);
+ SDIO_ITConfig(SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_DATAEND |
+ SDIO_IT_TXFIFOHE | SDIO_IT_RXFIFOHF | SDIO_IT_TXUNDERR |
+ SDIO_IT_RXOVERR | SDIO_IT_STBITERR, DISABLE);
+ NumberOfBytes = 0;
+ TransferError = SD_DATA_CRC_FAIL;
+ return(SD_DATA_CRC_FAIL);
+ }
+
+ if (SDIO_GetITStatus(SDIO_IT_DTIMEOUT) != RESET)
+ {
+ SDIO_ClearITPendingBit(SDIO_IT_DTIMEOUT);
+ SDIO_ITConfig(SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_DATAEND |
+ SDIO_IT_TXFIFOHE | SDIO_IT_RXFIFOHF | SDIO_IT_TXUNDERR |
+ SDIO_IT_RXOVERR | SDIO_IT_STBITERR, DISABLE);
+ NumberOfBytes = 0;
+ TransferError = SD_DATA_TIMEOUT;
+ return(SD_DATA_TIMEOUT);
+ }
+
+ if (SDIO_GetITStatus(SDIO_IT_RXOVERR) != RESET)
+ {
+ SDIO_ClearITPendingBit(SDIO_IT_RXOVERR);
+ SDIO_ITConfig(SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_DATAEND |
+ SDIO_IT_TXFIFOHE | SDIO_IT_RXFIFOHF | SDIO_IT_TXUNDERR |
+ SDIO_IT_RXOVERR | SDIO_IT_STBITERR, DISABLE);
+ NumberOfBytes = 0;
+ TransferError = SD_RX_OVERRUN;
+ return(SD_RX_OVERRUN);
+ }
+
+ if (SDIO_GetITStatus(SDIO_IT_TXUNDERR) != RESET)
+ {
+ SDIO_ClearITPendingBit(SDIO_IT_TXUNDERR);
+ SDIO_ITConfig(SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_DATAEND |
+ SDIO_IT_TXFIFOHE | SDIO_IT_RXFIFOHF | SDIO_IT_TXUNDERR |
+ SDIO_IT_RXOVERR | SDIO_IT_STBITERR, DISABLE);
+ NumberOfBytes = 0;
+ TransferError = SD_TX_UNDERRUN;
+ return(SD_TX_UNDERRUN);
+ }
+
+ if (SDIO_GetITStatus(SDIO_IT_STBITERR) != RESET)
+ {
+ SDIO_ClearITPendingBit(SDIO_IT_STBITERR);
+ SDIO_ITConfig(SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_DATAEND |
+ SDIO_IT_TXFIFOHE | SDIO_IT_RXFIFOHF | SDIO_IT_TXUNDERR |
+ SDIO_IT_RXOVERR | SDIO_IT_STBITERR, DISABLE);
+ NumberOfBytes = 0;
+ TransferError = SD_START_BIT_ERR;
+ return(SD_START_BIT_ERR);
+ }
+
+ return(SD_OK);
+}
+
+/**
+ * @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)
+{
+ SD_Error errorstatus = SD_OK;
+ uint32_t status;
+ uint32_t response_r1;
+
+ 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);
+ }
+
+ /* 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 for analysis */
+ response_r1 = SDIO_GetResponse(SDIO_RESP1);
+
+ if ((response_r1 & SD_OCR_ERRORBITS) == SD_ALLZERO)
+ {
+ return(errorstatus);
+ }
+
+ if (response_r1 & SD_OCR_ADDR_OUT_OF_RANGE)
+ {
+ return(SD_ADDR_OUT_OF_RANGE);
+ }
+
+ if (response_r1 & SD_OCR_ADDR_MISALIGNED)
+ {
+ return(SD_ADDR_MISALIGNED);
+ }
+
+ if (response_r1 & SD_OCR_BLOCK_LEN_ERR)
+ {
+ return(SD_BLOCK_LEN_ERR);
+ }
+
+ if (response_r1 & SD_OCR_ERASE_SEQ_ERR)
+ {
+ return(SD_ERASE_SEQ_ERR);
+ }
+
+ if (response_r1 & SD_OCR_BAD_ERASE_PARAM)
+ {
+ return(SD_BAD_ERASE_PARAM);
+ }
+
+ if (response_r1 & SD_OCR_WRITE_PROT_VIOLATION)
+ {
+ return(SD_WRITE_PROT_VIOLATION);
+ }
+
+ if (response_r1 & SD_OCR_LOCK_UNLOCK_FAILED)
+ {
+ return(SD_LOCK_UNLOCK_FAILED);
+ }
+
+ if (response_r1 & SD_OCR_COM_CRC_FAILED)
+ {
+ return(SD_COM_CRC_FAILED);
+ }
+
+ if (response_r1 & SD_OCR_ILLEGAL_CMD)
+ {
+ return(SD_ILLEGAL_CMD);
+ }
+
+ if (response_r1 & SD_OCR_CARD_ECC_FAILED)
+ {
+ return(SD_CARD_ECC_FAILED);
+ }
+
+ if (response_r1 & SD_OCR_CC_ERROR)
+ {
+ return(SD_CC_ERROR);
+ }
+
+ if (response_r1 & SD_OCR_GENERAL_UNKNOWN_ERROR)
+ {
+ return(SD_GENERAL_UNKNOWN_ERROR);
+ }
+
+ if (response_r1 & SD_OCR_STREAM_READ_UNDERRUN)
+ {
+ return(SD_STREAM_READ_UNDERRUN);
+ }
+
+ if (response_r1 & SD_OCR_STREAM_WRITE_OVERRUN)
+ {
+ return(SD_STREAM_WRITE_OVERRUN);
+ }
+
+ if (response_r1 & SD_OCR_CID_CSD_OVERWRIETE)
+ {
+ return(SD_CID_CSD_OVERWRITE);
+ }
+
+ if (response_r1 & SD_OCR_WP_ERASE_SKIP)
+ {
+ return(SD_WP_ERASE_SKIP);
+ }
+
+ if (response_r1 & SD_OCR_CARD_ECC_DISABLED)
+ {
+ return(SD_CARD_ECC_DISABLED);
+ }
+
+ if (response_r1 & SD_OCR_ERASE_RESET)
+ {
+ return(SD_ERASE_RESET);
+ }
+
+ if (response_r1 & SD_OCR_AKE_SEQ_ERROR)
+ {
+ return(SD_AKE_SEQ_ERROR);
+ }
+ return(errorstatus);
+}
+
+/**
+ * @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 = SDIO_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(SDIO_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 = SDIO_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(SDIO_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 = SDIO_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(SDIO_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 = SDIO_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(SDIO_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 = SDIO_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 != SDIO_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 = SDIO_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(SDIO_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 = SDIO_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(SDIO_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 = SDIO_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(SDIO_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
+ */
+static uint8_t convert_from_bytes_to_power_of_two(uint16_t NumberOfBytes)
+{
+ uint8_t count = 0;
+
+ while (NumberOfBytes != 1)
+ {
+ NumberOfBytes >>= 1;
+ count++;
+ }
+ return(count);
+}
+
+/**
+ * @brief Configures the SDIO Corresponding GPIO Ports
+ * @param None
+ * @retval None
+ */
+static void GPIO_Configuration(void)
+{
+ GPIO_InitTypeDef GPIO_InitStructure;
+
+ /* GPIOC and GPIOD Periph clock enable */
+ RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOD, ENABLE);
+
+ /* Configure PC.08, PC.09, PC.10, PC.11, PC.12 pin: D0, D1, D2, D3, CLK pin */
+ 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);
+
+ /* Configure PD.02 CMD line */
+ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
+ GPIO_Init(GPIOD, &GPIO_InitStructure);
+}
+
+/**
+ * @brief Configures the DMA2 Channel4 for SDIO Tx request.
+ * @param BufferSRC: pointer to the source buffer
+ * @param BufferSize: buffer size
+ * @retval None
+ */
+static void DMA_TxConfiguration(uint32_t *BufferSRC, uint32_t BufferSize)
+{
+ DMA_InitTypeDef DMA_InitStructure;
+
+ DMA_ClearFlag(DMA2_FLAG_TC4 | DMA2_FLAG_TE4 | DMA2_FLAG_HT4 | DMA2_FLAG_GL4);
+
+ /* DMA2 Channel4 disable */
+ DMA_Cmd(DMA2_Channel4, DISABLE);
+
+ /* DMA2 Channel4 Config */
+ 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);
- - /* DMA2 Channel4 enable */ - DMA_Cmd(DMA2_Channel4, ENABLE); -} - -/** - * @} - */ - -/** - * @} - */ - -/******************* (C) COPYRIGHT 2009 STMicroelectronics *****END OF FILE****/ - -/* - * RT-Thread SD Card Driver - * 20090417 Bernard - */ -#include <rtthread.h> -#include <dfs_fs.h> - -static struct rt_device sdcard_device; -static SD_CardInfo SDCardInfo; -static struct dfs_partition part; -static struct rt_semaphore sd_lock; - -/* RT-Thread Device Driver Interface */ -static rt_err_t rt_sdcard_init(rt_device_t dev) -{ - NVIC_InitTypeDef NVIC_InitStructure; - - NVIC_InitStructure.NVIC_IRQChannel = SDIO_IRQn; - NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; - NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; - NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; +
+ /* DMA2 Channel4 enable */
+ DMA_Cmd(DMA2_Channel4, ENABLE);
+}
+
+/**
+ * @brief Configures the DMA2 Channel4 for SDIO Rx request.
+ * @param BufferDST: pointer to the destination buffer
+ * @param BufferSize: buffer size
+ * @retval None
+ */
+static void DMA_RxConfiguration(uint32_t *BufferDST, uint32_t BufferSize)
+{
+ DMA_InitTypeDef DMA_InitStructure;
+
+ DMA_ClearFlag(DMA2_FLAG_TC4 | DMA2_FLAG_TE4 | DMA2_FLAG_HT4 | DMA2_FLAG_GL4);
+
+ /* DMA2 Channel4 disable */
+ DMA_Cmd(DMA2_Channel4, DISABLE);
+
+ /* DMA2 Channel4 Config */
+ 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);
+
+ /* DMA2 Channel4 enable */
+ DMA_Cmd(DMA2_Channel4, ENABLE);
+}
+
+/**
+ * @}
+ */
+
+/**
+ * @}
+ */
+
+/******************* (C) COPYRIGHT 2009 STMicroelectronics *****END OF FILE****/
+
+/*
+ * RT-Thread SD Card Driver
+ * 20090417 Bernard
+ */
+#include <rtthread.h>
+#include <dfs_fs.h>
+
+/* set sector size to 512 */
+#define SECTOR_SIZE 512
+
+static struct rt_device sdcard_device;
+static SD_CardInfo SDCardInfo;
+static struct dfs_partition part;
+static struct rt_semaphore sd_lock;
+static rt_uint8_t _sdcard_buffer[SECTOR_SIZE];
+
+/* RT-Thread Device Driver Interface */
+static rt_err_t rt_sdcard_init(rt_device_t dev)
+{
+ NVIC_InitTypeDef NVIC_InitStructure;
+
+ 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);
- - if (rt_sem_init(&sd_lock, "sdlock", 1, RT_IPC_FLAG_FIFO) != RT_EOK) - { - rt_kprintf("init sd lock semaphore failed\n"); - } - - return RT_EOK; -} - -static rt_err_t rt_sdcard_open(rt_device_t dev, rt_uint16_t oflag) -{ - - return RT_EOK; -} - -static rt_err_t rt_sdcard_close(rt_device_t dev) -{ - return RT_EOK; -} - -/* set sector size to 512 */ -#define SECTOR_SIZE 512 -static rt_size_t rt_sdcard_read(rt_device_t dev, rt_off_t pos, void* buffer, rt_size_t size) -{ - SD_Error status; - rt_uint32_t i, retry; - - // rt_kprintf("read: 0x%x, size %d\n", pos, size); - - rt_sem_take(&sd_lock, RT_WAITING_FOREVER); - retry = 3; +
+ if (rt_sem_init(&sd_lock, "sdlock", 1, RT_IPC_FLAG_FIFO) != RT_EOK)
+ {
+ rt_kprintf("init sd lock semaphore failed\n");
+ }
+
+ return RT_EOK;
+}
+
+static rt_err_t rt_sdcard_open(rt_device_t dev, rt_uint16_t oflag)
+{
+ return RT_EOK;
+}
+
+static rt_err_t rt_sdcard_close(rt_device_t dev)
+{
+ return RT_EOK;
+}
+
+static rt_size_t rt_sdcard_read(rt_device_t dev, rt_off_t pos, void* buffer, rt_size_t size)
+{
+ SD_Error status;
+ rt_uint32_t nr = size / SECTOR_SIZE;
+
+ rt_sem_take(&sd_lock, RT_WAITING_FOREVER);
+
/* read all sectors */
- for (i = 0; i < size / SECTOR_SIZE; i ++) - { -__retry: - status = SD_ReadBlock((part.offset + i)* SECTOR_SIZE + pos, - (uint32_t*)((rt_uint8_t*)buffer + i * SECTOR_SIZE), - SECTOR_SIZE); - if (status != SD_OK) + if (((rt_uint32_t)buffer % 4 != 0) ||
+ ((rt_uint32_t)buffer > 0x20080000))
+ {
+ rt_uint32_t index;
+
+ /* which is not alignment with 4 or chip SRAM */
+ for (index = 0; index < nr; index ++)
+ {
+ status = SD_ReadBlock((part.offset + index) * SECTOR_SIZE + pos,
+ (uint32_t*)_sdcard_buffer, SECTOR_SIZE);
+
+ if (status != SD_OK) break;
+
+ /* copy to the buffer */
+ rt_memcpy(((rt_uint8_t*)buffer + index * SECTOR_SIZE), _sdcard_buffer, SECTOR_SIZE);
+ }
+ }
+ else
+ {
+ if (nr == 1)
+ {
+ status = SD_ReadBlock(part.offset * SECTOR_SIZE + pos,
+ (uint32_t*)buffer, SECTOR_SIZE);
+ }
+ else
+ {
+ status = SD_ReadMultiBlocks(part.offset * SECTOR_SIZE + pos,
+ (uint32_t*)buffer, SECTOR_SIZE, nr);
+ }
+ }
+
+ rt_sem_release(&sd_lock);
+
+ if (status == SD_OK) return size;
+
+ rt_kprintf("read failed: %d, buffer 0x%08x\n", status, buffer);
+ return 0;
+}
+
+static rt_size_t rt_sdcard_write (rt_device_t dev, rt_off_t pos, const void* buffer, rt_size_t size)
+{
+ SD_Error status;
+ rt_uint32_t nr = size / SECTOR_SIZE;
+
+ rt_sem_take(&sd_lock, RT_WAITING_FOREVER);
+
+ /* read all sectors */
+ if (((rt_uint32_t)buffer % 4 != 0) ||
+ ((rt_uint32_t)buffer > 0x20080000))
+ {
+ rt_uint32_t index;
+
+ /* which is not alignment with 4 or chip SRAM */
+ for (index = 0; index < nr; index ++)
+ {
+ /* copy to the buffer */
+ rt_memcpy(_sdcard_buffer, ((rt_uint8_t*)buffer + index * SECTOR_SIZE), SECTOR_SIZE);
+
+ status = SD_WriteBlock((part.offset + index) * SECTOR_SIZE + pos,
+ (uint32_t*)_sdcard_buffer, SECTOR_SIZE);
+
+ if (status != SD_OK) break;
+ }
+ }
+ else
+ {
+ if (nr == 1)
+ {
+ status = SD_WriteBlock(part.offset * SECTOR_SIZE + pos,
+ (uint32_t*)buffer, SECTOR_SIZE);
+ }
+ else
+ {
+ status = SD_WriteMultiBlocks(part.offset * SECTOR_SIZE + pos,
+ (uint32_t*)buffer, SECTOR_SIZE, nr);
+ }
+ }
+
+ rt_sem_release(&sd_lock);
+
+ if (status == SD_OK) return size;
+
+ rt_kprintf("write failed: %d, buffer 0x%08x\n", status, buffer);
+ return 0;
+}
+
+static rt_err_t rt_sdcard_control(rt_device_t dev, rt_uint8_t cmd, void *args)
+{
+ return RT_EOK;
+}
+
+void rt_hw_sdcard_init()
+{
+ if (SD_Init() == SD_OK)
+ {
+ SD_Error status;
+ rt_uint8_t *sector;
+
+ status = SD_GetCardInfo(&SDCardInfo);
+ if (status != SD_OK) goto __return;
+
+ status = SD_SelectDeselect((u32) (SDCardInfo.RCA << 16));
+ if (status != SD_OK) goto __return;
+
+ SD_EnableWideBusOperation(SDIO_BusWide_4b);
+ SD_SetDeviceMode(SD_DMA_MODE);
+
+ /* get the first sector to read partition table */
+ sector = (rt_uint8_t*) rt_malloc (512);
+ if (sector == RT_NULL)
+ {
+ rt_kprintf("allocate partition sector buffer failed\n");
+ return;
+ }
+ status = SD_ReadBlock(0, (uint32_t*)sector, 512);
+ if (status == SD_OK)
{
- -- retry; - if (retry != 0) goto __retry; - else break; - } - } - - rt_sem_release(&sd_lock); - - if (status == SD_OK) return size; - - rt_kprintf("read failed: %d, buffer 0x%08x\n", status, buffer); - return 0; -} - -static rt_size_t rt_sdcard_write (rt_device_t dev, rt_off_t pos, const void* buffer, rt_size_t size) -{ - SD_Error status; - rt_uint32_t i; - - // rt_kprintf("write: 0x%x, size %d\n", pos, size); - - rt_sem_take(&sd_lock, RT_WAITING_FOREVER); - - /* write all sectors */ - for (i = 0; i < size / SECTOR_SIZE; i ++) - { - status = SD_WriteBlock((part.offset + i)* SECTOR_SIZE + pos, - (uint32_t*)((rt_uint8_t*)buffer + i * SECTOR_SIZE), - SECTOR_SIZE); - if (status != SD_OK) - { - rt_kprintf("sd card write failed\n"); - break; - } - } - - rt_sem_release(&sd_lock); - if (status == SD_OK) return size; - - rt_kprintf("write failed: %d, buffer 0x%08x\n", status, buffer); - return 0; -} - -static rt_err_t rt_sdcard_control(rt_device_t dev, rt_uint8_t cmd, void *args) -{ - return RT_EOK; -} - -void rt_hw_sdcard_init() -{ - if ( SD_Init() == SD_OK ) - { - SD_Error status; - rt_uint8_t *sector; - - SD_EnableWideBusOperation(SDIO_BusWide_1b); - - status = SD_GetCardInfo(&SDCardInfo); - if (status != SD_OK) goto __return; - - status = SD_SelectDeselect((u32) (SDCardInfo.RCA << 16)); - if (status != SD_OK) goto __return; - - SD_SetDeviceMode(SD_DMA_MODE); - - /* get the first sector to read partition table */ - sector = (rt_uint8_t*) rt_malloc (512); - if (sector == RT_NULL) - { - rt_kprintf("allocate partition sector buffer failed\n"); - return; - } - status = SD_ReadBlock(0, (uint32_t*)sector, 512); - if (status == SD_OK) - { - /* get the first partition */ - if (dfs_filesystem_get_partition(&part, sector, 0) != 0) - { - /* there is no partition */ - part.offset = 0; - part.size = 0; - } - } - else - { - /* there is no partition table */ - part.offset = 0; - part.size = 0; - } - - /* release sector buffer */ - rt_free(sector); - - /* register sdcard device */ - sdcard_device.init = rt_sdcard_init; - sdcard_device.open = rt_sdcard_open; - sdcard_device.close = rt_sdcard_close; - sdcard_device.read = rt_sdcard_read; - sdcard_device.write = rt_sdcard_write; - sdcard_device.control = rt_sdcard_control; - - /* no private */ - sdcard_device.private = &SDCardInfo; - - rt_device_register(&sdcard_device, "sd0", - RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_REMOVABLE | RT_DEVICE_FLAG_STANDALONE); - - return; - } - -__return: - rt_kprintf("sdcard init failed\n"); -} + /* get the first partition */
+ if (dfs_filesystem_get_partition(&part, sector, 0) != 0)
+ {
+ /* there is no partition */
+ part.offset = 0;
+ part.size = 0;
+ }
+ }
+ else
+ {
+ /* there is no partition table */
+ part.offset = 0;
+ part.size = 0;
+ }
+
+ /* release sector buffer */
+ rt_free(sector);
+
+ /* register sdcard device */
+ sdcard_device.init = rt_sdcard_init;
+ sdcard_device.open = rt_sdcard_open;
+ sdcard_device.close = rt_sdcard_close;
+ sdcard_device.read = rt_sdcard_read;
+ sdcard_device.write = rt_sdcard_write;
+ sdcard_device.control = rt_sdcard_control;
+
+ /* no private */
+ sdcard_device.private = &SDCardInfo;
+
+ rt_device_register(&sdcard_device, "sd0",
+ RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_REMOVABLE | RT_DEVICE_FLAG_STANDALONE);
+
+ return;
+ }
+
+__return:
+ rt_kprintf("sdcard init failed\n");
+}
diff --git a/bsp/stm32_radio/sdcard.h b/bsp/stm32_radio/sdcard.h index 386c12d965..fd89d94afb 100644 --- a/bsp/stm32_radio/sdcard.h +++ b/bsp/stm32_radio/sdcard.h @@ -1,285 +1,285 @@ -/** - ****************************************************************************** - * @file SDIO/sdcard.h - * @author MCD Application Team - * @version V3.1.0 - * @date 06/19/2009 - * @brief This file contains all the functions prototypes for the SD Card - * driver firmware library. - ****************************************************************************** - * @copy - * - * 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 2009 STMicroelectronics</center></h2> - */ - -/** @addtogroup STM32F10x_StdPeriph_Examples - * @{ - */ - -/** @addtogroup SDIO_Example - * @{ - */ - -/* Define to prevent recursive inclusion -------------------------------------*/ -#ifndef __SDCARD_H -#define __SDCARD_H - -/* Includes ------------------------------------------------------------------*/ -#include "stm32f10x.h" - -/* Exported types ------------------------------------------------------------*/ -typedef enum -{ - /* 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), - - /* 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, -} SD_Error; - -/* SDIO Commands Index */ -#define SDIO_GO_IDLE_STATE ((uint8_t)0) -#define SDIO_SEND_OP_COND ((uint8_t)1) -#define SDIO_ALL_SEND_CID ((uint8_t)2) -#define SDIO_SET_REL_ADDR ((uint8_t)3) /* SDIO_SEND_REL_ADDR for SD Card */ -#define SDIO_SET_DSR ((uint8_t)4) -#define SDIO_SDIO_SEN_OP_COND ((uint8_t)5) -#define SDIO_HS_SWITCH ((uint8_t)6) -#define SDIO_SEL_DESEL_CARD ((uint8_t)7) -#define SDIO_HS_SEND_EXT_CSD ((uint8_t)8) -#define SDIO_SEND_CSD ((uint8_t)9) -#define SDIO_SEND_CID ((uint8_t)10) -#define SDIO_READ_DAT_UNTIL_STOP ((uint8_t)11) /* SD Card doesn't support it */ -#define SDIO_STOP_TRANSMISSION ((uint8_t)12) -#define SDIO_SEND_STATUS ((uint8_t)13) -#define SDIO_HS_BUSTEST_READ ((uint8_t)14) -#define SDIO_GO_INACTIVE_STATE ((uint8_t)15) -#define SDIO_SET_BLOCKLEN ((uint8_t)16) -#define SDIO_READ_SINGLE_BLOCK ((uint8_t)17) -#define SDIO_READ_MULT_BLOCK ((uint8_t)18) -#define SDIO_HS_BUSTEST_WRITE ((uint8_t)19) -#define SDIO_WRITE_DAT_UNTIL_STOP ((uint8_t)20) /* SD Card doesn't support it */ -#define SDIO_SET_BLOCK_COUNT ((uint8_t)23) /* SD Card doesn't support it */ -#define SDIO_WRITE_SINGLE_BLOCK ((uint8_t)24) -#define SDIO_WRITE_MULT_BLOCK ((uint8_t)25) -#define SDIO_PROG_CID ((uint8_t)26) /* reserved for manufacturers */ -#define SDIO_PROG_CSD ((uint8_t)27) -#define SDIO_SET_WRITE_PROT ((uint8_t)28) -#define SDIO_CLR_WRITE_PROT ((uint8_t)29) -#define SDIO_SEND_WRITE_PROT ((uint8_t)30) -#define SDIO_SD_ERASE_GRP_START ((uint8_t)32) /* To set the address of the first write - block to be erased. (For SD card only) */ -#define SDIO_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 SDIO_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 SDIO_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 SDIO_ERASE ((uint8_t)38) -#define SDIO_FAST_IO ((uint8_t)39) /* SD Card doesn't support it */ -#define SDIO_GO_IRQ_STATE ((uint8_t)40) /* SD Card doesn't support it */ -#define SDIO_LOCK_UNLOCK ((uint8_t)42) -#define SDIO_APP_CMD ((uint8_t)55) -#define SDIO_GEN_CMD ((uint8_t)56) -#define SDIO_NO_CMD ((uint8_t)64) - -/* Following commands are SD Card Specific commands. - SDIO_APP_CMD should be sent before sending these - commands. */ -#define SDIO_APP_SD_SET_BUSWIDTH ((uint8_t)6) /* For SD Card only */ -#define SDIO_SD_APP_STAUS ((uint8_t)13) /* For SD Card only */ -#define SDIO_SD_APP_SEND_NUM_WRITE_BLOCKS ((uint8_t)22) /* For SD Card only */ -#define SDIO_SD_APP_OP_COND ((uint8_t)41) /* For SD Card only */ -#define SDIO_SD_APP_SET_CLR_CARD_DETECT ((uint8_t)42) /* For SD Card only */ -#define SDIO_SD_APP_SEND_SCR ((uint8_t)51) /* For SD Card only */ -#define SDIO_SDIO_RW_DIRECT ((uint8_t)52) /* For SD I/O Card only */ -#define SDIO_SDIO_RW_EXTENDED ((uint8_t)53) /* For SD I/O Card only */ - -/* Following commands are SD Card Specific security commands. - SDIO_APP_CMD should be sent before sending these commands. */ -#define SDIO_SD_APP_GET_MKB ((uint8_t)43) /* For SD Card only */ -#define SDIO_SD_APP_GET_MID ((uint8_t)44) /* For SD Card only */ -#define SDIO_SD_APP_SET_CER_RN1 ((uint8_t)45) /* For SD Card only */ -#define SDIO_SD_APP_GET_CER_RN2 ((uint8_t)46) /* For SD Card only */ -#define SDIO_SD_APP_SET_CER_RES2 ((uint8_t)47) /* For SD Card only */ -#define SDIO_SD_APP_GET_CER_RES1 ((uint8_t)48) /* For SD Card only */ -#define SDIO_SD_APP_SECURE_READ_MULTIPLE_BLOCK ((uint8_t)18) /* For SD Card only */ -#define SDIO_SD_APP_SECURE_WRITE_MULTIPLE_BLOCK ((uint8_t)25) /* For SD Card only */ -#define SDIO_SD_APP_SECURE_ERASE ((uint8_t)38) /* For SD Card only */ -#define SDIO_SD_APP_CHANGE_SECURE_AREA ((uint8_t)49) /* For SD Card only */ -#define SDIO_SD_APP_SECURE_WRITE_MKB ((uint8_t)48) /* For SD Card only */ - -typedef enum -{ - SD_NO_TRANSFER = 0, - SD_TRANSFER_IN_PROGRESS -} SDTransferState; - -typedef struct -{ - uint16_t TransferredBytes; - SD_Error TransferError; - uint8_t padding; -} SDLastTransferInfo; - -typedef struct /* Card Specific Data */ -{ - __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; - -typedef struct /*Card Identification Data*/ -{ - __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; - -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; - -/* Exported constants --------------------------------------------------------*/ -#define SD_DMA_MODE ((uint32_t)0x00000000) -#define SD_INTERRUPT_MODE ((uint32_t)0x00000001) -#define SD_POLLING_MODE ((uint32_t)0x00000002) - -/* Supported Memory Cards */ -#define SDIO_STD_CAPACITY_SD_CARD_V1_1 ((uint32_t)0x0) -#define SDIO_STD_CAPACITY_SD_CARD_V2_0 ((uint32_t)0x1) -#define SDIO_HIGH_CAPACITY_SD_CARD ((uint32_t)0x2) -#define SDIO_MULTIMEDIA_CARD ((uint32_t)0x3) -#define SDIO_SECURE_DIGITAL_IO_CARD ((uint32_t)0x4) -#define SDIO_HIGH_SPEED_MULTIMEDIA_CARD ((uint32_t)0x5) -#define SDIO_SECURE_DIGITAL_IO_COMBO_CARD ((uint32_t)0x6) -#define SDIO_HIGH_CAPACITY_MMC_CARD ((uint32_t)0x7) - -/* Exported macro ------------------------------------------------------------*/ -/* Exported functions ------------------------------------------------------- */ -SD_Error SD_Init(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_EnableWideBusOperation(uint32_t WideMode); -SD_Error SD_SetDeviceMode(uint32_t Mode); -SD_Error SD_SelectDeselect(uint32_t addr); -SD_Error SD_ReadBlock(uint32_t addr, uint32_t *readbuff, uint16_t BlockSize); -SD_Error SD_ReadMultiBlocks(uint32_t addr, uint32_t *readbuff, uint16_t BlockSize, uint32_t NumberOfBlocks); -SD_Error SD_WriteBlock(uint32_t addr, uint32_t *writebuff, uint16_t BlockSize); -SD_Error SD_WriteMultiBlocks(uint32_t addr, uint32_t *writebuff, 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); - -#endif /* __SDCARD_H */ - -/** - * @} - */ - -/** - * @} - */ - -/******************* (C) COPYRIGHT 2009 STMicroelectronics *****END OF FILE****/ +/**
+ ******************************************************************************
+ * @file SDIO/sdcard.h
+ * @author MCD Application Team
+ * @version V3.1.2
+ * @date 09/28/2009
+ * @brief This file contains all the functions prototypes for the SD Card
+ * driver firmware library.
+ ******************************************************************************
+ * @copy
+ *
+ * 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 2009 STMicroelectronics</center></h2>
+ */
+
+/** @addtogroup STM32F10x_StdPeriph_Examples
+ * @{
+ */
+
+/** @addtogroup SDIO_Example
+ * @{
+ */
+
+/* Define to prevent recursive inclusion -------------------------------------*/
+#ifndef __SDCARD_H
+#define __SDCARD_H
+
+/* Includes ------------------------------------------------------------------*/
+#include "stm32f10x.h"
+
+/* Exported types ------------------------------------------------------------*/
+typedef enum
+{
+ /* 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),
+
+ /* 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,
+} SD_Error;
+
+/* SDIO Commands Index */
+#define SDIO_GO_IDLE_STATE ((uint8_t)0)
+#define SDIO_SEND_OP_COND ((uint8_t)1)
+#define SDIO_ALL_SEND_CID ((uint8_t)2)
+#define SDIO_SET_REL_ADDR ((uint8_t)3) /* SDIO_SEND_REL_ADDR for SD Card */
+#define SDIO_SET_DSR ((uint8_t)4)
+#define SDIO_SDIO_SEN_OP_COND ((uint8_t)5)
+#define SDIO_HS_SWITCH ((uint8_t)6)
+#define SDIO_SEL_DESEL_CARD ((uint8_t)7)
+#define SDIO_HS_SEND_EXT_CSD ((uint8_t)8)
+#define SDIO_SEND_CSD ((uint8_t)9)
+#define SDIO_SEND_CID ((uint8_t)10)
+#define SDIO_READ_DAT_UNTIL_STOP ((uint8_t)11) /* SD Card doesn't support it */
+#define SDIO_STOP_TRANSMISSION ((uint8_t)12)
+#define SDIO_SEND_STATUS ((uint8_t)13)
+#define SDIO_HS_BUSTEST_READ ((uint8_t)14)
+#define SDIO_GO_INACTIVE_STATE ((uint8_t)15)
+#define SDIO_SET_BLOCKLEN ((uint8_t)16)
+#define SDIO_READ_SINGLE_BLOCK ((uint8_t)17)
+#define SDIO_READ_MULT_BLOCK ((uint8_t)18)
+#define SDIO_HS_BUSTEST_WRITE ((uint8_t)19)
+#define SDIO_WRITE_DAT_UNTIL_STOP ((uint8_t)20) /* SD Card doesn't support it */
+#define SDIO_SET_BLOCK_COUNT ((uint8_t)23) /* SD Card doesn't support it */
+#define SDIO_WRITE_SINGLE_BLOCK ((uint8_t)24)
+#define SDIO_WRITE_MULT_BLOCK ((uint8_t)25)
+#define SDIO_PROG_CID ((uint8_t)26) /* reserved for manufacturers */
+#define SDIO_PROG_CSD ((uint8_t)27)
+#define SDIO_SET_WRITE_PROT ((uint8_t)28)
+#define SDIO_CLR_WRITE_PROT ((uint8_t)29)
+#define SDIO_SEND_WRITE_PROT ((uint8_t)30)
+#define SDIO_SD_ERASE_GRP_START ((uint8_t)32) /* To set the address of the first write
+ block to be erased. (For SD card only) */
+#define SDIO_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 SDIO_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 SDIO_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 SDIO_ERASE ((uint8_t)38)
+#define SDIO_FAST_IO ((uint8_t)39) /* SD Card doesn't support it */
+#define SDIO_GO_IRQ_STATE ((uint8_t)40) /* SD Card doesn't support it */
+#define SDIO_LOCK_UNLOCK ((uint8_t)42)
+#define SDIO_APP_CMD ((uint8_t)55)
+#define SDIO_GEN_CMD ((uint8_t)56)
+#define SDIO_NO_CMD ((uint8_t)64)
+
+/* Following commands are SD Card Specific commands.
+ SDIO_APP_CMD should be sent before sending these
+ commands. */
+#define SDIO_APP_SD_SET_BUSWIDTH ((uint8_t)6) /* For SD Card only */
+#define SDIO_SD_APP_STAUS ((uint8_t)13) /* For SD Card only */
+#define SDIO_SD_APP_SEND_NUM_WRITE_BLOCKS ((uint8_t)22) /* For SD Card only */
+#define SDIO_SD_APP_OP_COND ((uint8_t)41) /* For SD Card only */
+#define SDIO_SD_APP_SET_CLR_CARD_DETECT ((uint8_t)42) /* For SD Card only */
+#define SDIO_SD_APP_SEND_SCR ((uint8_t)51) /* For SD Card only */
+#define SDIO_SDIO_RW_DIRECT ((uint8_t)52) /* For SD I/O Card only */
+#define SDIO_SDIO_RW_EXTENDED ((uint8_t)53) /* For SD I/O Card only */
+
+/* Following commands are SD Card Specific security commands.
+ SDIO_APP_CMD should be sent before sending these commands. */
+#define SDIO_SD_APP_GET_MKB ((uint8_t)43) /* For SD Card only */
+#define SDIO_SD_APP_GET_MID ((uint8_t)44) /* For SD Card only */
+#define SDIO_SD_APP_SET_CER_RN1 ((uint8_t)45) /* For SD Card only */
+#define SDIO_SD_APP_GET_CER_RN2 ((uint8_t)46) /* For SD Card only */
+#define SDIO_SD_APP_SET_CER_RES2 ((uint8_t)47) /* For SD Card only */
+#define SDIO_SD_APP_GET_CER_RES1 ((uint8_t)48) /* For SD Card only */
+#define SDIO_SD_APP_SECURE_READ_MULTIPLE_BLOCK ((uint8_t)18) /* For SD Card only */
+#define SDIO_SD_APP_SECURE_WRITE_MULTIPLE_BLOCK ((uint8_t)25) /* For SD Card only */
+#define SDIO_SD_APP_SECURE_ERASE ((uint8_t)38) /* For SD Card only */
+#define SDIO_SD_APP_CHANGE_SECURE_AREA ((uint8_t)49) /* For SD Card only */
+#define SDIO_SD_APP_SECURE_WRITE_MKB ((uint8_t)48) /* For SD Card only */
+
+typedef enum
+{
+ SD_NO_TRANSFER = 0,
+ SD_TRANSFER_IN_PROGRESS
+} SDTransferState;
+
+typedef struct
+{
+ uint16_t TransferredBytes;
+ SD_Error TransferError;
+ uint8_t padding;
+} SDLastTransferInfo;
+
+typedef struct /* Card Specific Data */
+{
+ __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;
+
+typedef struct /*Card Identification Data*/
+{
+ __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;
+
+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;
+
+/* Exported constants --------------------------------------------------------*/
+#define SD_DMA_MODE ((uint32_t)0x00000000)
+#define SD_INTERRUPT_MODE ((uint32_t)0x00000001)
+#define SD_POLLING_MODE ((uint32_t)0x00000002)
+
+/* Supported Memory Cards */
+#define SDIO_STD_CAPACITY_SD_CARD_V1_1 ((uint32_t)0x0)
+#define SDIO_STD_CAPACITY_SD_CARD_V2_0 ((uint32_t)0x1)
+#define SDIO_HIGH_CAPACITY_SD_CARD ((uint32_t)0x2)
+#define SDIO_MULTIMEDIA_CARD ((uint32_t)0x3)
+#define SDIO_SECURE_DIGITAL_IO_CARD ((uint32_t)0x4)
+#define SDIO_HIGH_SPEED_MULTIMEDIA_CARD ((uint32_t)0x5)
+#define SDIO_SECURE_DIGITAL_IO_COMBO_CARD ((uint32_t)0x6)
+#define SDIO_HIGH_CAPACITY_MMC_CARD ((uint32_t)0x7)
+
+/* Exported macro ------------------------------------------------------------*/
+/* Exported functions ------------------------------------------------------- */
+SD_Error SD_Init(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_EnableWideBusOperation(uint32_t WideMode);
+SD_Error SD_SetDeviceMode(uint32_t Mode);
+SD_Error SD_SelectDeselect(uint32_t addr);
+SD_Error SD_ReadBlock(uint32_t addr, uint32_t *readbuff, uint16_t BlockSize);
+SD_Error SD_ReadMultiBlocks(uint32_t addr, uint32_t *readbuff, uint16_t BlockSize, uint32_t NumberOfBlocks);
+SD_Error SD_WriteBlock(uint32_t addr, uint32_t *writebuff, uint16_t BlockSize);
+SD_Error SD_WriteMultiBlocks(uint32_t addr, uint32_t *writebuff, 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);
+
+#endif /* __SDCARD_H */
+
+/**
+ * @}
+ */
+
+/**
+ * @}
+ */
+
+/******************* (C) COPYRIGHT 2009 STMicroelectronics *****END OF FILE****/
diff --git a/bsp/stm32_radio/usart.h b/bsp/stm32_radio/usart.h index ae74ffde71..36c5f19b1e 100644 --- a/bsp/stm32_radio/usart.h +++ b/bsp/stm32_radio/usart.h @@ -1,3 +1,17 @@ +/*
+ * File : usart.h
+ * This file is part of RT-Thread RTOS
+ * COPYRIGHT (C) 2009, RT-Thread Development Team
+ *
+ * The license and distribution terms for this file may be
+ * found in the file LICENSE in this distribution or at
+ * http://www.rt-thread.org/license/LICENSE
+ *
+ * Change Logs:
+ * Date Author Notes
+ * 2009-01-05 Bernard the first version
+ */
+
#ifndef __USART_H__
#define __USART_H__
|
