⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.c

📁 STM32读取SD卡上FAT12_16_32文件系统源代码
💻 C
字号:
/******************** (C) COPYRIGHT 2008 STMicroelectronics ********************
 * File Name          : main.c
 * Author             : MCD Application Team
 * Version            : V2.0.2
 * Date               : 07/11/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 "stm32f10x_lib.h"
#include "usart.h"
#include "ff.h"
#include "diskio.h"

#define NULL 0
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/

void RCC_Config(void);
void NVIC_Config(void); 
void TIM2_Config(void);

/*******************************************************************************
 * Function Name  : main
 * Description    : Main program.
 * Input          : None
 * Output         : None
 * Return         : None
 *******************************************************************************/


int main(void)
{
    unsigned int i;
    BYTE buffer[512];    // file copy buffer
    FATFS fs;            // Work area (file system object) for logical drive
    FIL fsrc;            // file objects
    FRESULT res;         // FatFs function common result code
    UINT br;             // File R/W count
#ifdef DEBUG
    debug();
#endif
    RCC_Config();
    NVIC_Config(); 
    TIM2_Config();
    USART_Config();

    
    USART1_Puts("Now, I'll read file 'i2c/uart.lst'.\n");

    // Register a work area for logical drive 0
    f_mount(0, &fs);

    // Open source file
    res = f_open(&fsrc, "i2c/uart.lst", FA_OPEN_EXISTING | FA_READ);
    if (res) 
    {
        USART1_Puts("Can't open i2c/uart.lst for read. :-(\n");
        goto exit;
    }
    
    for (;;) {
        res = f_read(&fsrc, buffer, sizeof(buffer), &br);
        if (res || br == 0) break;   // error or eof
        for( i = 0; i < br; ++i )
            USART1_Putc(buffer[i]);
    }
       
    f_close(&fsrc);
exit:
    // Unregister a work area before discard it
    f_mount(0, NULL);

    /* Infinite loop */
    while (1)
    {
    }
}
//******************************************************************************
// Function Name  : RCC_Configuration
// Description    : Reset and Clock Control configuration
// Input          : None
// Output         : None
// Return         : None
//******************************************************************************
void RCC_Config(void)
{
    ErrorStatus HSEStartUpStatus;

    // Reset the RCC clock configuration to default reset state
    RCC_DeInit();

    // Configures the High Speed External oscillator
    RCC_HSEConfig(RCC_HSE_ON);

    // Waits for HSE start-up
    HSEStartUpStatus = RCC_WaitForHSEStartUp();

    if(HSEStartUpStatus == SUCCESS)
    {
        // Enable Prefetch Buffer
        FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);

        // Sets the code latency value: FLASH Two Latency cycles
        FLASH_SetLatency(FLASH_Latency_2);

        // Configures the AHB clock(HCLK): HCLK = SYSCLK
        RCC_HCLKConfig(RCC_SYSCLK_Div1);

        // Configures the High Speed APB clcok(PCLK2): PCLK2 = HCLK
        RCC_PCLK2Config(RCC_HCLK_Div1);

        // Configures the Low Speed APB clock(PCLK1): PCLK1 = HCLK/2
        RCC_PCLK1Config(RCC_HCLK_Div2);

        // Configures the PLL clock source and multiplication factor
        // PLLCLK = HSE*PLLMul = 8*9 = 72MHz
        RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);

        // Enable PLL
        RCC_PLLCmd(ENABLE);

        // Checks whether the specified RCC flag is set or not
        // Wait till PLL is ready
        while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET);

        // Select PLL as system clock source
        RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);

        // Get System Clock Source
        // Wait till PLL is used as system clock source
        while(RCC_GetSYSCLKSource() != 0x08);
    }    
}

//******************************************************************************
// Function Name  : NVIC_Configuration
// Description    : Nested Vectored Interrupt Controller configuration
// Input          : None
// Output         : None
// Return         : None
//******************************************************************************
void NVIC_Config(void)
{
    NVIC_InitTypeDef NVIC_InitStructure; 
#ifdef VECT_TAB_RAM
    // Set the Vector Tab base at location at 0x20000000
    NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
#else
    // Set the Vector Tab base at location at 0x80000000
    NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);
#endif   
    /* Configure the NVIC Preemption Priority Bits[配置优先级组] */  
    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);

    /* Enable the TIM2 gloabal Interrupt [允许TIM2全局中断]*/
    NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQChannel;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;  
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure); 

}

void TIM2_Config(void)
{
    TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
    //  TIM_OCInitTypeDef  TIM_OCInitStructure ;
    TIM_DeInit( TIM2);                              //复位TIM2定时器

    /* TIM2 clock enable [TIM2定时器允许]*/
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);

    /* TIM2 configuration */
    TIM_TimeBaseStructure.TIM_Period = 20;          //       
    TIM_TimeBaseStructure.TIM_Prescaler = 35999;    // 72M/(35999+1)/20 = 100Hz       
    TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;  // 时钟分割  
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;  //计数方向向上计数
    TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);

    /* Clear TIM2 update pending flag[清除TIM2溢出中断标志] */
    TIM_ClearFlag(TIM2, TIM_FLAG_Update);

    /* Enable TIM2 Update interrupt [TIM2溢出中断允许]*/
    TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);  

    /* TIM2 enable counter [允许tim2计数]*/
    TIM_Cmd(TIM2, ENABLE);
}

#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****/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -