找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 2265|回复: 0
打印 上一主题 下一主题
收起左侧

stm32 SD卡测试程序

[复制链接]
跳转到指定楼层
楼主
stm32F103ZET6 SD卡测试程序送上
  1. /******************** (C) COPYRIGHT 2008 STMicroelectronics ********************
  2. * File Name          : main.c
  3. * Author             : MCD Application Team
  4. * Version            : V3.5
  5. * Date               : 09/22/2008
  6. * Description        : Main program body.
  7. ********************************************************************************
  8. * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  9. * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME.
  10. * AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT,
  11. * INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE
  12. * CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING
  13. * INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  14. *******************************************************************************/

  15. /* Includes ------------------------------------------------------------------*/
  16. #include "lcd.h"
  17. #include "stm32f10x.h"
  18. #include "platform_config.h"
  19. #include <stdio.h>

  20. #include "sdcard.h"
  21. #include "integer.h"
  22. #include "ff.h"
  23. #include "diskio.h"

  24. /* Private typedef -----------------------------------------------------------*/
  25. /* Private define ------------------------------------------------------------*/
  26. /* Private macro -------------------------------------------------------------*/
  27. /* Private variables ---------------------------------------------------------*/
  28. static vu32 TimingDelay = 0;
  29. GPIO_InitTypeDef GPIO_InitStructure;
  30. ErrorStatus HSEStartUpStatus;
  31. u32  index = 0;
  32. SD_Error Status = SD_OK;
  33. SD_CardInfo SDCardInfo;
  34. FATFS fs;            // Work area (file system object) for logical drive
  35. FRESULT res;         // FatFs function common result code
  36. UINT br, bw;         // File R/W count
  37. FIL fsrc, fdst;      // file objects
  38. BYTE buffer[51];          // file copy buffer
  39. BYTE buff_filename[9][13];        //最多只能读9个文件,保存9个文件名,文件名采用短文件名,最多13个字符
  40. USART_InitTypeDef USART_InitStructure;


  41. u8 stringcopy(BYTE *buff_to,BYTE *buff_from)
  42. {
  43.   u8 i=0;
  44.   for(i=0;i<13;i++)
  45.   buff_to[i] = buff_from[i];
  46.   return 1;
  47. }
  48. void ReadSDFile(void)
  49. {
  50.   FIL file;
  51.   FILINFO finfo;
  52.   DIR dirs;
  53.   int i_name=0;
  54.   char *fn;
  55.   char path[50]={""};
  56.   disk_initialize(0);
  57.   f_mount(0, &fs);
  58.   res =  f_opendir(&dirs, path);
  59.   if (res == FR_OK)
  60.   {
  61.     while (f_readdir(&dirs, &finfo) == FR_OK)
  62.         {
  63.           if (finfo.fattrib & AM_ARC)
  64.       {
  65.         if(!finfo.fname[0])          //文件名不为空,如果为空,则表明该目录下面的文件已经读完了
  66.           break;         
  67.         res = f_open(&fsrc, finfo.fname, FA_OPEN_EXISTING | FA_READ);
  68.                 stringcopy(buff_filename[i_name], (BYTE*)finfo.fname);
  69.                 i_name++;
  70. //        res = f_read(&fsrc, &buffer, 50, &br);
  71.         f_close(&fsrc);
  72.           }
  73.         }
  74.   }
  75. }

  76. extern u8 Image_Table[];

  77. /* Private function prototypes -----------------------------------------------*/
  78. void RCC_Configuration(void);
  79. void InterruptConfig(void);
  80. void NVIC_Configuration(void);
  81. void USART_Configuration(void);
  82. void Delay(vu32 nCount);
  83. void Decrement_TimingDelay(void);
  84. void SysTick_Configuration(void);

  85. /* Private functions ---------------------------------------------------------*/
  86. SD_Error SD_InitAndConfig(void)
  87. {
  88.   Status = SD_Init();
  89.   if (Status == SD_OK)
  90.   {
  91.           LCD_DisplayStringLine(Line0,"SD_Init success",White,Black);
  92.   }
  93.   else
  94.   {
  95.           LCD_DisplayStringLine(Line0,"SD_Init fail",White,Black);
  96.   }
  97.   Delay(200);
  98.   if (Status == SD_OK)
  99.   {
  100.     /*----------------- Read CSD/CID MSD registers ------------------*/
  101.     Status = SD_GetCardInfo(&SDCardInfo);
  102.   }
  103.   
  104.   if (Status == SD_OK)
  105.   {
  106.     /*----------------- Select Card --------------------------------*/
  107.     Status = SD_SelectDeselect((u32) (SDCardInfo.RCA << 16));
  108.   }
  109.   
  110.   if (Status == SD_OK)
  111.   {
  112.     Status = SD_EnableWideBusOperation(SDIO_BusWide_1b);
  113.   }
  114.   
  115.   /* Set Device Transfer Mode to INTERRUPT */
  116.   if (Status == SD_OK)
  117.   {  
  118.     Status = SD_SetDeviceMode(SD_INTERRUPT_MODE);
  119.   }
  120.   return Status;
  121. }
  122. /*******************************************************************************
  123. * Function Name  : main
  124. * Description    : Main program.
  125. * Input          : None
  126. * Output         : None
  127. * Return         : None
  128. *******************************************************************************/
  129. int main(void)
  130. {
  131. #ifdef DEBUG
  132.   debug();
  133. #endif
  134.   /* System Clocks Configuration */
  135.   RCC_Configuration();   

  136.   /* Interrupt Configuration */
  137.   InterruptConfig();

  138. //  USART_Configuration();

  139.   /* Configure the systick */   
  140.   SysTick_Configuration();

  141.   /* Initialize the LCD */
  142.   STM3210E_LCD_Init();

  143.   /* Clear the LCD */
  144.   LCD_Clear(Black);

  145.   /*初始化并设置SDIO接口*/
  146.   Status =SD_InitAndConfig();

  147.   /* Clear the LCD */
  148.   LCD_Clear(Black);
  149. //  printf("hello\n");


  150. //经过测试,一行LCD以16*24的大小来显示的话,只能显示20个字符
  151. //一共可以显示10行,编号从Line0到Line9,每行的高度是24,字符的实际高度比24要小
  152. //LCD_DisplayStringLine(Line0,"12345678901234567890",White,Black);         
  153.   LCD_DisplayStringLine(Line3,"     Waiting....    ",White,Black);
  154.   LCD_DisplayStringLine(Line4,"   Read SD Card...  ",White,Black);

  155.   ReadSDFile();  
  156.   Delay(100);
  157.   /* Clear the LCD */
  158.   LCD_Clear(Black);
  159.   if (Status == SD_OK)
  160.           {
  161.                 LCD_DisplayStringLine(Line0,"File read success",White,Black);
  162.         }
  163.         else
  164.         {
  165.                 LCD_DisplayStringLine(Line0,"File read Fail",White,Black);               
  166.         }
  167.   Delay(100);
  168.   /* Clear the LCD */
  169.   LCD_Clear(Black);
  170.   LCD_DisplayStringLine(Line1,buff_filename[0],White,Black);
  171.   LCD_DisplayStringLine(Line2,buff_filename[1],White,Black);
  172.   
  173.   //LCD_DisplayStringLine(Line3,"                  ",White,Black);
  174.   //LCD_DisplayStringLine(Line4,"                  ",White,Black);
  175.   
  176.   LCD_DisplayStringLine(Line3,buff_filename[2],White,Black);
  177.   LCD_DisplayStringLine(Line4,buff_filename[3],White,Black);  

  178.   /* Configure IO connected to LD1, LD2, LD3 LD4 and LD5 leds *********************/
  179.   /* Enable GPIO_LED clock */
  180.   RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIO_LED, ENABLE);

  181.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10;
  182.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  183.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  184.   GPIO_Init(GPIO_LED, &GPIO_InitStructure);

  185.   while (1)
  186.   {
  187. //    /* Turn on LD1 */
  188. //    GPIO_ResetBits(GPIO_LED, GPIO_Pin_6);
  189. //    /* Insert delay */
  190. //    Delay(20);
  191. //
  192. //    /* Turn on LD2 and LD3 */
  193. //    GPIO_ResetBits(GPIO_LED, GPIO_Pin_7 | GPIO_Pin_8);
  194. //    /* Turn off LD1 */
  195. //    GPIO_SetBits(GPIO_LED, GPIO_Pin_6);
  196. //    /* Insert delay */
  197. //    Delay(20);
  198. //
  199. //    /* Turn on LD4 and LD5 */
  200. //    GPIO_ResetBits(GPIO_LED, GPIO_Pin_9 | GPIO_Pin_10);
  201. //    /* Turn off LD2 and LD3 */
  202. //    GPIO_SetBits(GPIO_LED, GPIO_Pin_7 | GPIO_Pin_8);
  203. //    /* Insert delay */
  204. //    Delay(20);
  205. //
  206. //    /* Turn off LD4 and LD5 */
  207. //    GPIO_SetBits(GPIO_LED, GPIO_Pin_9 | GPIO_Pin_10);
  208. //        LCD_Clear(Black+index);
  209. //        Delay(100);
  210. //        index = index + 10;
  211. //        if(index >=        0xFFFF)index = 0;
  212.   }
  213. }

  214. /*******************************************************************************
  215. * Function Name  : RCC_Configuration
  216. * Description    : Configures the different system clocks.
  217. * Input          : None
  218. * Output         : None
  219. * Return         : None
  220. *******************************************************************************/
  221. void RCC_Configuration(void)
  222. {   
  223.   /* RCC system reset(for debug purpose) */
  224.   RCC_DeInit();

  225.   /* Enable HSE */
  226.   RCC_HSEConfig(RCC_HSE_ON);

  227.   /* Wait till HSE is ready */
  228.   HSEStartUpStatus = RCC_WaitForHSEStartUp();

  229.   if(HSEStartUpStatus == SUCCESS)
  230.   {
  231.     /* Enable Prefetch Buffer */
  232.     FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);

  233.     /* Flash 2 wait state */
  234.     FLASH_SetLatency(FLASH_Latency_2);
  235.        
  236.     /* HCLK = SYSCLK */
  237.     RCC_HCLKConfig(RCC_SYSCLK_Div1);
  238.   
  239.     /* PCLK2 = HCLK */
  240.     RCC_PCLK2Config(RCC_HCLK_Div1);

  241.     /* PCLK1 = HCLK/2 */
  242.     RCC_PCLK1Config(RCC_HCLK_Div2);

  243.     /* PLLCLK = 8MHz * 9 = 72 MHz */
  244.     RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);

  245.     /* Enable PLL */
  246.     RCC_PLLCmd(ENABLE);

  247.     /* Wait till PLL is ready */
  248.     while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
  249.     {
  250.     }

  251.     /* Select PLL as system clock source */
  252.     RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);

  253.     /* Wait till PLL is used as system clock source */
  254.     while(RCC_GetSYSCLKSource() != 0x08)
  255.     {
  256.     }
  257.   }
  258. }

  259. /*******************************************************************************
  260. * Function Name  : NVIC_Configuration
  261. * Description    : Configures Vector Table base location.
  262. * Input          : None
  263. * Output         : None
  264. * Return         : None
  265. *******************************************************************************/
  266. void NVIC_Configuration(void)
  267. {
  268. //------------zp2000--------------------------------
  269.   NVIC_InitTypeDef NVIC_InitStructure;

  270.   /* Configure the NVIC Preemption Priority Bits */
  271.   NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);//该函数调用了两次

  272.   NVIC_InitStructure.NVIC_IRQChannel = SDIO_IRQn;
  273.   NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  274.   NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  275.   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  276.   NVIC_Init(&NVIC_InitStructure);

  277. #ifdef  VECT_TAB_RAM  
  278.   /* Set the Vector Table base location at 0x20000000 */
  279.   NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
  280. #else  /* VECT_TAB_FLASH  */
  281.   /* Set the Vector Table base location at 0x08000000 */
  282.   NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);   
  283. #endif

  284. }

  285. void USART_Configuration(void)
  286. {
  287.   GPIO_InitTypeDef GPIO_InitStructure;

  288.   /* Configure USART1 Tx (PA.09) as alternate function push-pull */
  289.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
  290.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  291.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  292.   GPIO_Init(GPIOA, &GPIO_InitStructure);
  293.    
  294.   /* Configure USART1 Rx (PA.10) as input floating */
  295.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
  296.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  297.   GPIO_Init(GPIOA, &GPIO_InitStructure);

  298.   
  299.         USART_InitStructure.USART_BaudRate = 115200;
  300.         USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  301.         USART_InitStructure.USART_StopBits = USART_StopBits_1;
  302.         USART_InitStructure.USART_Parity = USART_Parity_No ;
  303.         USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  304.        
  305.        
  306.         USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
  307.         /* Configure USART1 basic and asynchronous paramters */
  308.         USART_Init(USART1, &USART_InitStructure);
  309.    
  310.   /* Enable USART1 */
  311.   USART_Cmd(USART1, ENABLE);
  312. }

  313. /*******************************************************************************
  314. * Function Name  : InterruptConfig
  315. * Description    : Configures the used IRQ Channels and sets their priority.
  316. * Input          : None
  317. * Output         : None
  318. * Return         : None
  319. *******************************************************************************/
  320. void InterruptConfig(void)
  321. {
  322.   /* Deinitializes the NVIC */
  323. //  NVIC_DeInit();

  324.   NVIC_Configuration();

  325.   /* Configure the Priority Group to 2 bits */
  326. //  NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);               

  327.   /* Configure the SysTick handler priority */
  328.   //为了使用SDIO中断,下面的中断优先级被我改低了
  329. //  NVIC_SystemHandlerPriorityConfig(SystemHandler_SysTick, 1, 1);
  330. }

  331. /*******************************************************************************
  332. * Function Name  : Delay
  333. * Description    : Inserts a delay time.
  334. * Input          : nCount: specifies the delay time length (time base 10 ms).
  335. * Output         : None
  336. * Return         : None
  337. *******************************************************************************/
  338. void Delay(u32 nCount)
  339. {
  340.   TimingDelay = nCount;

  341.   SysTick->LOAD  = (72000 & SysTick_LOAD_RELOAD_Msk) - 1;      /* set reload register */
  342.   NVIC_SetPriority (SysTick_IRQn, 9);  /* set Priority for Cortex-M0 System Interrupts */
  343.   SysTick->VAL   = 0;                                          /* Load the SysTick Counter Value */
  344.   SysTick->CTRL  = SysTick_CTRL_CLKSOURCE_Msk |
  345.                    SysTick_CTRL_TICKINT_Msk   |
  346.                    SysTick_CTRL_ENABLE_Msk;  
  347.   while(TimingDelay != 0)
  348.   {
  349.   }
  350.   SysTick->CTRL &= ~SysTick_CTRL_ENABLE_Msk;
  351.   
  352. }

  353. /*******************************************************************************
  354. * Function Name  : Decrement_TimingDelay
  355. * Description    : Decrements the TimingDelay variable.
  356. * Input          : None
  357. * Output         : TimingDelay
  358. * Return         : None
  359. *******************************************************************************/
  360. void Decrement_TimingDelay(void)
  361. {
  362.   if (TimingDelay != 0x00)
  363.   {
  364.     TimingDelay--;
  365.   }
  366. }

  367. /*******************************************************************************
  368. * Function Name  : SysTick_Config
  369. * Description    : Configure a SysTick Base time to 10 ms.
  370. * Input          : None
  371. * Output         : None
  372. * Return         : None
  373. *******************************************************************************/
  374. void SysTick_Configuration(void)
  375. {
  376.   /* Configure HCLK clock as SysTick clock source */
  377.   SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK);

  378.   NVIC_SetPriority (SysTick_IRQn, 9);  /* set Priority for Cortex-M0 System Interrupts */
  379.   SysTick->CTRL &= ~SysTick_CTRL_ENABLE_Msk;
  380. }

  381. int fputc(int ch, FILE *f)
  382. {
  383.   /* Place your implementation of fputc here */
  384.   /* e.g. write a character to the USART */
  385.   USART_SendData(USART1, (u8) ch);

  386.   /* Loop until the end of transmission */
  387.   while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET)
  388.   {
  389.   }

  390.   return ch;
  391. }

  392. #ifdef  DEBUG
  393. /*******************************************************************************
  394. * Function Name  : assert_failed
  395. * Description    : Reports the name of the source file and the source line number
  396. *                  where the assert_param error has occurred.
  397. * Input          : - file: pointer to the source file name
  398. *                  - line: assert_param error line source number
  399. * Output         : None
  400. * Return         : None
  401. *******************************************************************************/
  402. void assert_failed(u8* file, u32 line)
  403. {
  404.   /* User can add his own implementation to report the file name and line number,
  405.      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

  406.   /* Infinite loop */
  407.   while (1)
  408.   {
  409.   }
  410. }
  411. #endif

  412. /******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/
复制代码



主程序预览:

【24】红牛板_SD卡测试(SDIO).rar

6.41 MB, 下载次数: 24, 下载积分: 黑币 -5

SD卡测试

评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表