/* USER CODE BEGIN Header */
/**
************************************************************************
******
* @file : main.c
* @brief : Main program body
************************************************************************
******
* @attention
*
* <h2><center>© Copyright (c) 2020 STMicroelectronics.
* All rights reserved.</center></h2>
*
* This software component is licensed by ST under Ultimate Liberty
license
* SLA0044, the "License"; You may not use this file except in
compliance with
* the License. You may obtain a copy of the License at:
* www.st.com/SLA0044
*
************************************************************************
******
*/
/* USER CODE END Header */
/* Includes
------------------------------------------------------------------*/
#include "main.h"
#include "fatfs.h"
#include "usb_device.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;
SPI_HandleTypeDef hspi1;
UART_HandleTypeDef huart1;
UART_HandleTypeDef huart2;
/* USER CODE BEGIN PV */
/* USER CODE END PV */
/* Private function prototypes
-----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_SPI1_Init(void);
static void MX_SDIO_SD_Init(void);
static void MX_USART1_UART_Init(void);
static void MX_USART2_UART_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 */
/* 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_SPI1_Init();
MX_SDIO_SD_Init();
MX_USART1_UART_Init();
MX_USART2_UART_Init();
MX_FATFS_Init();
MX_USB_DEVICE_Init();
/* USER CODE BEGIN 2 */
FATFS sdFatFs; // File system object for SD disk logical drive
char sdPath[4]; // SD disk logical drive path
FIL myFile; // File object
FRESULT res = FR_OK; // FatFs function common result code
uint8_t wtext[] = "Hello world"; // File write buffer
uint8_t rtext[100]; // File read buffer
uint32_t byteswritten, bytesread; // File write/read counts
res = f_mount(&sdFatFs, (TCHAR const*) sdPath, 0);
if (res != FR_OK) {
// Fehler beim Mounten
return -1;
}
res = f_open(&myFile, "TEST.TXT", FA_CREATE_ALWAYS | FA_WRITE);
if (res != FR_OK) {
// Fehler beim Erstellen oder Öffnen der Datei
return -1;
}
res = f_write(&myFile, wtext, sizeof(wtext),(void *) &byteswritten);
if (res != FR_OK) {
// Fehler beim Beschreiben der Datei
return -1;
}
f_close(&myFile); // Schliessen der Datei
res = f_open(&myFile, "TEST.TXT", FA_READ);
if (res != FR_OK) {
// Fehler beim Öffnen der Datei
return -1;
}
res = f_read(&myFile, rtext, sizeof(rtext),(UINT*) &bytesread);
if ((bytesread == 0) || (res != FR_OK)) {
// Fehler beim Lesen aus der Datei
return -1;
}
f_close(&myFile); // Schliessen der Datei
FATFS_UnLinkDriver(sdPath); // Trennen des SDIO-Treibers von der
Middleware-Komponente
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}