Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* USER CODE BEGIN Header */
- /**
- ******************************************************************************
- * @file : main.c
- * @brief : Main program body
- ******************************************************************************
- * @attention
- *
- * Copyright (c) 2024 STMicroelectronics.
- * All rights reserved.
- *
- * This software is licensed under terms that can be found in the LICENSE file
- * in the root directory of this software component.
- * If no LICENSE file comes with this software, it is provided AS-IS.
- *
- ******************************************************************************
- */
- /* USER CODE END Header */
- /* Includes ------------------------------------------------------------------*/
- #include "main.h"
- #include "fatfs.h"
- /* Private includes ----------------------------------------------------------*/
- /* USER CODE BEGIN Includes */
- /* USER CODE END Includes */
- /* Private typedef -----------------------------------------------------------*/
- /* USER CODE BEGIN PTD */
- /* USER CODE END PTD */
- /* Private define ------------------------------------------------------------*/
- /* USER CODE BEGIN PD */
- /* USER CODE END PD */
- /* Private macro -------------------------------------------------------------*/
- /* USER CODE BEGIN PM */
- /* USER CODE END PM */
- /* Private variables ---------------------------------------------------------*/
- SD_HandleTypeDef hsd;
- DMA_HandleTypeDef hdma_sdio_rx;
- DMA_HandleTypeDef hdma_sdio_tx;
- /* USER CODE BEGIN PV */
- FATFS SDFatFs; /* File system object for SD card logical drive */
- FIL MyFile; /* File object */
- extern char SDPath[4]; /* SD card logical drive path */
- static uint8_t buffer[_MAX_SS]; /* a work buffer for the f_mkfs() */
- /* USER CODE END PV */
- /* Private function prototypes -----------------------------------------------*/
- void SystemClock_Config(void);
- static void MX_GPIO_Init(void);
- static void MX_DMA_Init(void);
- static void MX_SDIO_SD_Init(void);
- /* USER CODE BEGIN PFP */
- /* USER CODE END PFP */
- /* Private user code ---------------------------------------------------------*/
- /* USER CODE BEGIN 0 */
- /* USER CODE END 0 */
- /**
- * @brief The application entry point.
- * @retval int
- */
- int main(void)
- {
- /* USER CODE BEGIN 1 */
- FRESULT res; /* FatFs function common result code */
- uint32_t byteswritten, bytesread; /* File write/read counts */
- uint8_t wtext[] = "This is STM32 working with FatFs"; /* File write buffer */
- uint8_t rtext[100]; /* File read buffer */
- /* USER CODE END 1 */
- /* MCU Configuration--------------------------------------------------------*/
- /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
- HAL_Init();
- /* USER CODE BEGIN Init */
- /* USER CODE END Init */
- /* Configure the system clock */
- SystemClock_Config();
- /* USER CODE BEGIN SysInit */
- /* USER CODE END SysInit */
- /* Initialize all configured peripherals */
- MX_GPIO_Init();
- MX_DMA_Init();
- MX_SDIO_SD_Init();
- MX_FATFS_Init();
- /* USER CODE BEGIN 2 */
- if(FATFS_LinkDriver(&SD_Driver, SDPath) == 0)
- {
- /*##-2- Register the file system object to the FatFs module ##############*/
- if(f_mount(&SDFatFs, (TCHAR const*)SDPath, 1) != FR_OK)
- {
- /* FatFs Initialization Error */
- Error_Handler();
- }
- else
- {
- /*##-3- Create a FAT file system (format) on the logical drive #########*/
- /* WARNING: Formatting the uSD card will delete all content on the device */
- //if(f_mkfs((TCHAR const*)SDPath, FM_ANY, 0, buffer, sizeof(buffer)) != FR_OK)
- if(0)
- {
- /* FatFs Format Error */
- Error_Handler();
- }
- else
- {
- /*##-4- Create and Open a new text file object with write access #####*/
- if(f_open(&MyFile, "STM32.TXT", FA_CREATE_ALWAYS | FA_WRITE) != FR_OK)
- {
- /* 'STM32.TXT' file Open for write Error */
- Error_Handler();
- }
- else
- {
- /*##-5- Write data to the text file ################################*/
- res = f_write(&MyFile, wtext, sizeof(wtext), (void *)&byteswritten);
- if((byteswritten == 0) || (res != FR_OK))
- {
- /* 'STM32.TXT' file Write or EOF Error */
- Error_Handler();
- }
- else
- {
- /*##-6- Close the open text file #################################*/
- f_close(&MyFile);
- /*##-7- Open the text file object with read access ###############*/
- if(f_open(&MyFile, "STM32.TXT", FA_READ) != FR_OK)
- {
- /* 'STM32.TXT' file Open for read Error */
- Error_Handler();
- }
- else
- {
- /*##-8- Read data from the text file ###########################*/
- res = f_read(&MyFile, rtext, sizeof(rtext), (UINT*)&bytesread);
- if((bytesread == 0) || (res != FR_OK))
- {
- /* 'STM32.TXT' file Read or EOF Error */
- Error_Handler();
- }
- else
- {
- /*##-9- Close the open text file #############################*/
- f_close(&MyFile);
- /*##-10- Compare read data with the expected data ############*/
- if((bytesread != byteswritten))
- {
- /* Read data is different from the expected data */
- Error_Handler();
- }
- else
- {
- /* Success of the demo: no error occurrence */
- HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_SET);
- }
- }
- }
- }
- }
- }
- }
- }
- /* USER CODE END 2 */
- /* Infinite loop */
- /* USER CODE BEGIN WHILE */
- //HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_SET);
- while (1)
- {
- /* USER CODE END WHILE */
- /* USER CODE BEGIN 3 */
- }
- /* USER CODE END 3 */
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement