|
stm32F103ZET6 SD卡測(cè)試程序送上
- /******************** (C) COPYRIGHT 2008 STMicroelectronics ********************
- * File Name : main.c
- * Author : MCD Application Team
- * Version : V3.5
- * Date : 09/22/2008
- * Description : Main program body.
- ********************************************************************************
- * 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.
- *******************************************************************************/
- /* Includes ------------------------------------------------------------------*/
- #include "lcd.h"
- #include "stm32f10x.h"
- #include "platform_config.h"
- #include <stdio.h>
- #include "sdcard.h"
- #include "integer.h"
- #include "ff.h"
- #include "diskio.h"
- /* Private typedef -----------------------------------------------------------*/
- /* Private define ------------------------------------------------------------*/
- /* Private macro -------------------------------------------------------------*/
- /* Private variables ---------------------------------------------------------*/
- static vu32 TimingDelay = 0;
- GPIO_InitTypeDef GPIO_InitStructure;
- ErrorStatus HSEStartUpStatus;
- u32 index = 0;
- SD_Error Status = SD_OK;
- SD_CardInfo SDCardInfo;
- FATFS fs; // Work area (file system object) for logical drive
- FRESULT res; // FatFs function common result code
- UINT br, bw; // File R/W count
- FIL fsrc, fdst; // file objects
- BYTE buffer[51]; // file copy buffer
- BYTE buff_filename[9][13]; //最多只能讀9個(gè)文件,保存9個(gè)文件名,文件名采用短文件名,最多13個(gè)字符
- USART_InitTypeDef USART_InitStructure;
- u8 stringcopy(BYTE *buff_to,BYTE *buff_from)
- {
- u8 i=0;
- for(i=0;i<13;i++)
- buff_to[i] = buff_from[i];
- return 1;
- }
- void ReadSDFile(void)
- {
- FIL file;
- FILINFO finfo;
- DIR dirs;
- int i_name=0;
- char *fn;
- char path[50]={""};
- disk_initialize(0);
- f_mount(0, &fs);
- res = f_opendir(&dirs, path);
- if (res == FR_OK)
- {
- while (f_readdir(&dirs, &finfo) == FR_OK)
- {
- if (finfo.fattrib & AM_ARC)
- {
- if(!finfo.fname[0]) //文件名不為空,如果為空,則表明該目錄下面的文件已經(jīng)讀完了
- break;
- res = f_open(&fsrc, finfo.fname, FA_OPEN_EXISTING | FA_READ);
- stringcopy(buff_filename[i_name], (BYTE*)finfo.fname);
- i_name++;
- // res = f_read(&fsrc, &buffer, 50, &br);
- f_close(&fsrc);
- }
- }
- }
- }
- extern u8 Image_Table[];
- /* Private function prototypes -----------------------------------------------*/
- void RCC_Configuration(void);
- void InterruptConfig(void);
- void NVIC_Configuration(void);
- void USART_Configuration(void);
- void Delay(vu32 nCount);
- void Decrement_TimingDelay(void);
- void SysTick_Configuration(void);
- /* Private functions ---------------------------------------------------------*/
- SD_Error SD_InitAndConfig(void)
- {
- Status = SD_Init();
- if (Status == SD_OK)
- {
- LCD_DisplayStringLine(Line0,"SD_Init success",White,Black);
- }
- else
- {
- LCD_DisplayStringLine(Line0,"SD_Init fail",White,Black);
- }
- Delay(200);
- if (Status == SD_OK)
- {
- /*----------------- Read CSD/CID MSD registers ------------------*/
- Status = SD_GetCardInfo(&SDCardInfo);
- }
-
- if (Status == SD_OK)
- {
- /*----------------- Select Card --------------------------------*/
- Status = SD_SelectDeselect((u32) (SDCardInfo.RCA << 16));
- }
-
- if (Status == SD_OK)
- {
- Status = SD_EnableWideBusOperation(SDIO_BusWide_1b);
- }
-
- /* Set Device Transfer Mode to INTERRUPT */
- if (Status == SD_OK)
- {
- Status = SD_SetDeviceMode(SD_INTERRUPT_MODE);
- }
- return Status;
- }
- /*******************************************************************************
- * Function Name : main
- * Description : Main program.
- * Input : None
- * Output : None
- * Return : None
- *******************************************************************************/
- int main(void)
- {
- #ifdef DEBUG
- debug();
- #endif
- /* System Clocks Configuration */
- RCC_Configuration();
- /* Interrupt Configuration */
- InterruptConfig();
- // USART_Configuration();
- /* Configure the systick */
- SysTick_Configuration();
- /* Initialize the LCD */
- STM3210E_LCD_Init();
- /* Clear the LCD */
- LCD_Clear(Black);
- /*初始化并設(shè)置SDIO接口*/
- Status =SD_InitAndConfig();
- /* Clear the LCD */
- LCD_Clear(Black);
- // printf("hello\n");
- //經(jīng)過(guò)測(cè)試,一行LCD以16*24的大小來(lái)顯示的話(huà),只能顯示20個(gè)字符
- //一共可以顯示10行,編號(hào)從Line0到Line9,每行的高度是24,字符的實(shí)際高度比24要小
- //LCD_DisplayStringLine(Line0,"12345678901234567890",White,Black);
- LCD_DisplayStringLine(Line3," Waiting.... ",White,Black);
- LCD_DisplayStringLine(Line4," Read SD Card... ",White,Black);
- ReadSDFile();
- Delay(100);
- /* Clear the LCD */
- LCD_Clear(Black);
- if (Status == SD_OK)
- {
- LCD_DisplayStringLine(Line0,"File read success",White,Black);
- }
- else
- {
- LCD_DisplayStringLine(Line0,"File read Fail",White,Black);
- }
- Delay(100);
- /* Clear the LCD */
- LCD_Clear(Black);
- LCD_DisplayStringLine(Line1,buff_filename[0],White,Black);
- LCD_DisplayStringLine(Line2,buff_filename[1],White,Black);
-
- //LCD_DisplayStringLine(Line3," ",White,Black);
- //LCD_DisplayStringLine(Line4," ",White,Black);
-
- LCD_DisplayStringLine(Line3,buff_filename[2],White,Black);
- LCD_DisplayStringLine(Line4,buff_filename[3],White,Black);
- /* Configure IO connected to LD1, LD2, LD3 LD4 and LD5 leds *********************/
- /* Enable GPIO_LED clock */
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIO_LED, ENABLE);
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIO_LED, &GPIO_InitStructure);
- while (1)
- {
- // /* Turn on LD1 */
- // GPIO_ResetBits(GPIO_LED, GPIO_Pin_6);
- // /* Insert delay */
- // Delay(20);
- //
- // /* Turn on LD2 and LD3 */
- // GPIO_ResetBits(GPIO_LED, GPIO_Pin_7 | GPIO_Pin_8);
- // /* Turn off LD1 */
- // GPIO_SetBits(GPIO_LED, GPIO_Pin_6);
- // /* Insert delay */
- // Delay(20);
- //
- // /* Turn on LD4 and LD5 */
- // GPIO_ResetBits(GPIO_LED, GPIO_Pin_9 | GPIO_Pin_10);
- // /* Turn off LD2 and LD3 */
- // GPIO_SetBits(GPIO_LED, GPIO_Pin_7 | GPIO_Pin_8);
- // /* Insert delay */
- // Delay(20);
- //
- // /* Turn off LD4 and LD5 */
- // GPIO_SetBits(GPIO_LED, GPIO_Pin_9 | GPIO_Pin_10);
- // LCD_Clear(Black+index);
- // Delay(100);
- // index = index + 10;
- // if(index >= 0xFFFF)index = 0;
- }
- }
- /*******************************************************************************
- * Function Name : RCC_Configuration
- * Description : Configures the different system clocks.
- * Input : None
- * Output : None
- * Return : None
- *******************************************************************************/
- void RCC_Configuration(void)
- {
- /* RCC system reset(for debug purpose) */
- RCC_DeInit();
- /* Enable HSE */
- RCC_HSEConfig(RCC_HSE_ON);
- /* Wait till HSE is ready */
- HSEStartUpStatus = RCC_WaitForHSEStartUp();
- if(HSEStartUpStatus == SUCCESS)
- {
- /* Enable Prefetch Buffer */
- FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
- /* Flash 2 wait state */
- FLASH_SetLatency(FLASH_Latency_2);
-
- /* HCLK = SYSCLK */
- RCC_HCLKConfig(RCC_SYSCLK_Div1);
-
- /* PCLK2 = HCLK */
- RCC_PCLK2Config(RCC_HCLK_Div1);
- /* PCLK1 = HCLK/2 */
- RCC_PCLK1Config(RCC_HCLK_Div2);
- /* PLLCLK = 8MHz * 9 = 72 MHz */
- RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);
- /* Enable PLL */
- RCC_PLLCmd(ENABLE);
- /* Wait till PLL is ready */
- while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
- {
- }
- /* Select PLL as system clock source */
- RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
- /* Wait till PLL is used as system clock source */
- while(RCC_GetSYSCLKSource() != 0x08)
- {
- }
- }
- }
- /*******************************************************************************
- * Function Name : NVIC_Configuration
- * Description : Configures Vector Table base location.
- * Input : None
- * Output : None
- * Return : None
- *******************************************************************************/
- void NVIC_Configuration(void)
- {
- //------------zp2000--------------------------------
- NVIC_InitTypeDef NVIC_InitStructure;
- /* Configure the NVIC Preemption Priority Bits */
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);//該函數(shù)調(diào)用了兩次
- 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);
- #ifdef VECT_TAB_RAM
- /* Set the Vector Table base location at 0x20000000 */
- NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
- #else /* VECT_TAB_FLASH */
- /* Set the Vector Table base location at 0x08000000 */
- NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);
- #endif
- }
- void USART_Configuration(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- /* Configure USART1 Tx (PA.09) as alternate function push-pull */
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
-
- /* Configure USART1 Rx (PA.10) as input floating */
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
-
- USART_InitStructure.USART_BaudRate = 115200;
- USART_InitStructure.USART_WordLength = USART_WordLength_8b;
- USART_InitStructure.USART_StopBits = USART_StopBits_1;
- USART_InitStructure.USART_Parity = USART_Parity_No ;
- USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
-
-
- USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
- /* Configure USART1 basic and asynchronous paramters */
- USART_Init(USART1, &USART_InitStructure);
-
- /* Enable USART1 */
- USART_Cmd(USART1, ENABLE);
- }
- /*******************************************************************************
- * Function Name : InterruptConfig
- * Description : Configures the used IRQ Channels and sets their priority.
- * Input : None
- * Output : None
- * Return : None
- *******************************************************************************/
- void InterruptConfig(void)
- {
- /* Deinitializes the NVIC */
- // NVIC_DeInit();
- NVIC_Configuration();
- /* Configure the Priority Group to 2 bits */
- // NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
- /* Configure the SysTick handler priority */
- //為了使用SDIO中斷,下面的中斷優(yōu)先級(jí)被我改低了
- // NVIC_SystemHandlerPriorityConfig(SystemHandler_SysTick, 1, 1);
- }
- /*******************************************************************************
- * Function Name : Delay
- * Description : Inserts a delay time.
- * Input : nCount: specifies the delay time length (time base 10 ms).
- * Output : None
- * Return : None
- *******************************************************************************/
- void Delay(u32 nCount)
- {
- TimingDelay = nCount;
- SysTick->LOAD = (72000 & SysTick_LOAD_RELOAD_Msk) - 1; /* set reload register */
- NVIC_SetPriority (SysTick_IRQn, 9); /* set Priority for Cortex-M0 System Interrupts */
- SysTick->VAL = 0; /* Load the SysTick Counter Value */
- SysTick->CTRL = SysTick_CTRL_CLKSOURCE_Msk |
- SysTick_CTRL_TICKINT_Msk |
- SysTick_CTRL_ENABLE_Msk;
- while(TimingDelay != 0)
- {
- }
- SysTick->CTRL &= ~SysTick_CTRL_ENABLE_Msk;
-
- }
- /*******************************************************************************
- * Function Name : Decrement_TimingDelay
- * Description : Decrements the TimingDelay variable.
- * Input : None
- * Output : TimingDelay
- * Return : None
- *******************************************************************************/
- void Decrement_TimingDelay(void)
- {
- if (TimingDelay != 0x00)
- {
- TimingDelay--;
- }
- }
- /*******************************************************************************
- * Function Name : SysTick_Config
- * Description : Configure a SysTick Base time to 10 ms.
- * Input : None
- * Output : None
- * Return : None
- *******************************************************************************/
- void SysTick_Configuration(void)
- {
- /* Configure HCLK clock as SysTick clock source */
- SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK);
-
- NVIC_SetPriority (SysTick_IRQn, 9); /* set Priority for Cortex-M0 System Interrupts */
- SysTick->CTRL &= ~SysTick_CTRL_ENABLE_Msk;
- }
- int fputc(int ch, FILE *f)
- {
- /* Place your implementation of fputc here */
- /* e.g. write a character to the USART */
- USART_SendData(USART1, (u8) ch);
- /* Loop until the end of transmission */
- while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET)
- {
- }
- return ch;
- }
- #ifdef DEBUG
- /*******************************************************************************
- * Function Name : assert_failed
- * Description : Reports the name of the source file and the source line number
- * where the assert_param error has occurred.
- * Input : - file: pointer to the source file name
- * - line: assert_param error line source number
- * Output : None
- * Return : None
- *******************************************************************************/
- void assert_failed(u8* file, u32 line)
- {
- /* User can add his own implementation to report the file name and line number,
- ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
- /* Infinite loop */
- while (1)
- {
- }
- }
- #endif
- /******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/
復(fù)制代碼
主程序預(yù)覽:
|
評(píng)分
-
查看全部評(píng)分
|