initial software commit
This commit is contained in:
709
Src/File_Handling.c
Normal file
709
Src/File_Handling.c
Normal file
@ -0,0 +1,709 @@
|
||||
/*
|
||||
* File_Handling_RTOS.c
|
||||
*
|
||||
* Created on: 14-May-2020
|
||||
* Author: Controllerstech
|
||||
*/
|
||||
|
||||
#include <File_Handling.h>
|
||||
#include "stm32f7xx_hal.h"
|
||||
|
||||
#define UART USART1
|
||||
|
||||
|
||||
|
||||
/* =============================>>>>>>>> NO CHANGES AFTER THIS LINE =====================================>>>>>>> */
|
||||
|
||||
FATFS fs; // file system
|
||||
FIL fil; // File
|
||||
FILINFO fno;
|
||||
extern FRESULT fresult; // result
|
||||
extern unsigned long sizeoffile;
|
||||
UINT br, bw; // File read/write count
|
||||
|
||||
/**** capacity related *****/
|
||||
FATFS *pfs;
|
||||
DWORD fre_clust;
|
||||
uint32_t total, free_space;
|
||||
|
||||
|
||||
void Send_Uart (char *string)
|
||||
{
|
||||
//HAL_UART_Transmit(UART, (uint8_t *)string, strlen (string), HAL_MAX_DELAY);
|
||||
}
|
||||
|
||||
|
||||
|
||||
int Mount_SD (const TCHAR* path)
|
||||
{
|
||||
fresult = f_mount(&fs, path, 1);
|
||||
if (fresult != FR_OK) return 1;
|
||||
else return 0;
|
||||
}
|
||||
|
||||
int Unmount_SD (const TCHAR* path)
|
||||
{
|
||||
fresult = f_mount(NULL, path, 1);
|
||||
if (fresult == FR_OK) return 0;//Send_Uart ("SD CARD UNMOUNTED successfully...\n\n\n");
|
||||
return 1;//else Send_Uart("ERROR!!! in UNMOUNTING SD CARD\n\n\n");
|
||||
}
|
||||
|
||||
/* Start node to be scanned (***also used as work area***) */
|
||||
FRESULT Scan_SD (char* pat)
|
||||
{
|
||||
DIR dir;
|
||||
UINT i;
|
||||
char *path = malloc(20*sizeof (char));
|
||||
sprintf (path, "%s",pat);
|
||||
|
||||
fresult = f_opendir(&dir, path); /* Open the directory */
|
||||
if (fresult == FR_OK)
|
||||
{
|
||||
for (;;)
|
||||
{
|
||||
fresult = f_readdir(&dir, &fno); /* Read a directory item */
|
||||
if (fresult != FR_OK || fno.fname[0] == 0) break; /* Break on error or end of dir */
|
||||
if (fno.fattrib & AM_DIR) /* It is a directory */
|
||||
{
|
||||
if (!(strcmp ("SYSTEM~1", fno.fname))) continue;
|
||||
char *buf = malloc(30*sizeof(char));
|
||||
sprintf (buf, "Dir: %s\r\n", fno.fname);
|
||||
Send_Uart(buf);
|
||||
free(buf);
|
||||
i = strlen(path);
|
||||
sprintf(&path[i], "/%s", fno.fname);
|
||||
fresult = Scan_SD(path); /* Enter the directory */
|
||||
if (fresult != FR_OK) break;
|
||||
path[i] = 0;
|
||||
}
|
||||
else
|
||||
{ /* It is a file. */
|
||||
char *buf = malloc(30*sizeof(char));
|
||||
sprintf(buf,"File: %s/%s\n", path, fno.fname);
|
||||
Send_Uart(buf);
|
||||
free(buf);
|
||||
}
|
||||
}
|
||||
f_closedir(&dir);
|
||||
}
|
||||
free(path);
|
||||
return fresult;
|
||||
}
|
||||
|
||||
/* Only supports removing files from home directory */
|
||||
FRESULT Format_SD (void)
|
||||
{
|
||||
DIR dir;
|
||||
char *path = malloc(20*sizeof (char));
|
||||
sprintf (path, "%s","/");
|
||||
|
||||
fresult = f_opendir(&dir, path); /* Open the directory */
|
||||
if (fresult == FR_OK)
|
||||
{
|
||||
for (;;)
|
||||
{
|
||||
fresult = f_readdir(&dir, &fno); /* Read a directory item */
|
||||
if (fresult != FR_OK || fno.fname[0] == 0) break; /* Break on error or end of dir */
|
||||
if (fno.fattrib & AM_DIR) /* It is a directory */
|
||||
{
|
||||
if (!(strcmp ("SYSTEM~1", fno.fname))) continue;
|
||||
fresult = f_unlink(fno.fname);
|
||||
if (fresult == FR_DENIED) continue;
|
||||
}
|
||||
else
|
||||
{ /* It is a file. */
|
||||
fresult = f_unlink(fno.fname);
|
||||
}
|
||||
}
|
||||
f_closedir(&dir);
|
||||
}
|
||||
free(path);
|
||||
return fresult;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
FRESULT Write_File (char *name, char *data)
|
||||
{
|
||||
|
||||
/**** check whether the file exists or not ****/
|
||||
fresult = f_stat (name, &fno);
|
||||
if (fresult != FR_OK)
|
||||
{
|
||||
char *buf = malloc(100*sizeof(char));
|
||||
//sprintf (buf, "ERROR!!! *%s* does not exists\n\n", name);
|
||||
//Send_Uart (buf);
|
||||
free(buf);
|
||||
return fresult;
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
/* Create a file with read write access and open it */
|
||||
fresult = f_open(&fil, name, FA_OPEN_EXISTING | FA_WRITE);
|
||||
if (fresult != FR_OK)
|
||||
{
|
||||
char *buf = malloc(100*sizeof(char));
|
||||
//sprintf (buf, "ERROR!!! No. %d in opening file *%s*\n\n", fresult, name);
|
||||
//Send_Uart(buf);
|
||||
free(buf);
|
||||
return fresult;
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
fresult = f_write(&fil, data, strlen(data), &bw);
|
||||
if (fresult != FR_OK)
|
||||
{
|
||||
char *buf = malloc(100*sizeof(char));
|
||||
//sprintf (buf, "ERROR!!! No. %d while writing to the FILE *%s*\n\n", fresult, name);
|
||||
//Send_Uart(buf);
|
||||
free(buf);
|
||||
}
|
||||
|
||||
/* Close file */
|
||||
fresult = f_close(&fil);
|
||||
if (fresult != FR_OK)
|
||||
{
|
||||
char *buf = malloc(100*sizeof(char));
|
||||
//sprintf (buf, "ERROR!!! No. %d in closing file *%s* after writing it\n\n", fresult, name);
|
||||
//Send_Uart(buf);
|
||||
free(buf);
|
||||
}
|
||||
else
|
||||
{
|
||||
char *buf = malloc(100*sizeof(char));
|
||||
//sprintf (buf, "File *%s* is WRITTEN and CLOSED successfully\n", name);
|
||||
//Send_Uart(buf);
|
||||
free(buf);
|
||||
}
|
||||
}
|
||||
return fresult;
|
||||
}
|
||||
}
|
||||
|
||||
FRESULT Write_File_byte (char *name, uint8_t *data, unsigned int bytesize)
|
||||
{
|
||||
|
||||
/**** check whether the file exists or not ****/
|
||||
fresult = f_stat (name, &fno);
|
||||
if (fresult != FR_OK)
|
||||
{
|
||||
char *buf = malloc(100*sizeof(char));
|
||||
//sprintf (buf, "ERROR!!! *%s* does not exists\n\n", name);
|
||||
//Send_Uart (buf);
|
||||
free(buf);
|
||||
return fresult;
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
/* Create a file with read write access and open it */
|
||||
fresult = f_open(&fil, name, FA_OPEN_EXISTING | FA_WRITE);
|
||||
if (fresult != FR_OK)
|
||||
{
|
||||
char *buf = malloc(100*sizeof(char));
|
||||
//sprintf (buf, "ERROR!!! No. %d in opening file *%s*\n\n", fresult, name);
|
||||
//Send_Uart(buf);
|
||||
free(buf);
|
||||
return fresult;
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
fresult = f_write(&fil, data, bytesize, &bw);
|
||||
if (fresult != FR_OK)
|
||||
{
|
||||
char *buf = malloc(100*sizeof(char));
|
||||
//sprintf (buf, "ERROR!!! No. %d while writing to the FILE *%s*\n\n", fresult, name);
|
||||
//Send_Uart(buf);
|
||||
free(buf);
|
||||
}
|
||||
|
||||
/* Close file */
|
||||
fresult = f_close(&fil);
|
||||
if (fresult != FR_OK)
|
||||
{
|
||||
char *buf = malloc(100*sizeof(char));
|
||||
//sprintf (buf, "ERROR!!! No. %d in closing file *%s* after writing it\n\n", fresult, name);
|
||||
//Send_Uart(buf);
|
||||
free(buf);
|
||||
}
|
||||
else
|
||||
{
|
||||
char *buf = malloc(100*sizeof(char));
|
||||
//sprintf (buf, "File *%s* is WRITTEN and CLOSED successfully\n", name);
|
||||
//Send_Uart(buf);
|
||||
free(buf);
|
||||
}
|
||||
}
|
||||
return fresult;
|
||||
}
|
||||
}
|
||||
|
||||
FRESULT Read_File (char *name)
|
||||
{
|
||||
/**** check whether the file exists or not ****/
|
||||
fresult = f_stat (name, &fno);
|
||||
if (fresult != FR_OK)
|
||||
{
|
||||
char *buf = malloc(100*sizeof(char));
|
||||
sprintf (buf, "ERRROR!!! *%s* does not exists\n\n", name);
|
||||
Send_Uart (buf);
|
||||
free(buf);
|
||||
return fresult;
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
/* Open file to read */
|
||||
fresult = f_open(&fil, name, FA_READ);
|
||||
|
||||
if (fresult != FR_OK)
|
||||
{
|
||||
char *buf = malloc(100*sizeof(char));
|
||||
sprintf (buf, "ERROR!!! No. %d in opening file *%s*\n\n", fresult, name);
|
||||
Send_Uart(buf);
|
||||
free(buf);
|
||||
return fresult;
|
||||
}
|
||||
|
||||
/* Read data from the file
|
||||
* see the function details for the arguments */
|
||||
|
||||
char *buffer = malloc(sizeof(f_size(&fil)));
|
||||
fresult = f_read (&fil, buffer, f_size(&fil), &br);
|
||||
if (fresult != FR_OK)
|
||||
{
|
||||
char *buf = malloc(100*sizeof(char));
|
||||
free(buffer);
|
||||
sprintf (buf, "ERROR!!! No. %d in reading file *%s*\n\n", fresult, name);
|
||||
Send_Uart(buffer);
|
||||
free(buf);
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
Send_Uart(buffer);
|
||||
free(buffer);
|
||||
|
||||
/* Close file */
|
||||
fresult = f_close(&fil);
|
||||
if (fresult != FR_OK)
|
||||
{
|
||||
char *buf = malloc(100*sizeof(char));
|
||||
sprintf (buf, "ERROR!!! No. %d in closing file *%s*\n\n", fresult, name);
|
||||
Send_Uart(buf);
|
||||
free(buf);
|
||||
}
|
||||
else
|
||||
{
|
||||
char *buf = malloc(100*sizeof(char));
|
||||
sprintf (buf, "File *%s* CLOSED successfully\n", name);
|
||||
Send_Uart(buf);
|
||||
free(buf);
|
||||
}
|
||||
}
|
||||
return fresult;
|
||||
}
|
||||
}
|
||||
|
||||
FRESULT Seek_Read_File (char *name, uint8_t *data, unsigned int bytesize, unsigned long goto_label)
|
||||
{
|
||||
/**** check whether the file exists or not ****/
|
||||
fresult = f_stat (name, &fno);
|
||||
if (fresult != FR_OK)
|
||||
{
|
||||
char *buf = malloc(100*sizeof(char));
|
||||
sprintf (buf, "ERRROR!!! *%s* does not exists\n\n", name);
|
||||
//Send_Uart (buf);
|
||||
free(buf);
|
||||
return fresult;
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
/* Open file to read */
|
||||
fresult = f_open(&fil, name, FA_READ);
|
||||
|
||||
if (fresult != FR_OK)
|
||||
{
|
||||
char *buf = malloc(100*sizeof(char));
|
||||
sprintf (buf, "ERROR!!! No. %d in opening file *%s*\n\n", fresult, name);
|
||||
//Send_Uart(buf);
|
||||
free(buf);
|
||||
return fresult;
|
||||
}
|
||||
|
||||
/* Read data from the file
|
||||
* see the function details for the arguments */
|
||||
|
||||
//char *buffer = malloc(sizeof(f_size(&fil)));
|
||||
fresult = f_lseek (&fil, goto_label); /* Move file pointer of the file object */
|
||||
if (fresult != FR_OK)
|
||||
{
|
||||
char *buf = malloc(100*sizeof(char));
|
||||
//free(buffer);
|
||||
sprintf (buf, "ERROR!!! Can't seek the file: *%s*\n\n", name);
|
||||
//Send_Uart(buffer);
|
||||
free(buf);
|
||||
return fresult;
|
||||
}
|
||||
fresult = f_read (&fil, data, bytesize, &br);
|
||||
if (fresult != FR_OK)
|
||||
{
|
||||
char *buf = malloc(100*sizeof(char));
|
||||
//free(buffer);
|
||||
sprintf (buf, "ERROR!!! No. %d in reading file *%s*\n\n", fresult, name);
|
||||
//Send_Uart(buffer);
|
||||
free(buf);
|
||||
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
//Send_Uart(buffer);
|
||||
//free(buffer);
|
||||
if (goto_label==0)//Set size of file in first 4 bytes
|
||||
{
|
||||
sizeoffile = f_size(&fil);
|
||||
data[0] = (uint8_t) (sizeoffile&0xff);
|
||||
data[1] = (uint8_t) ((sizeoffile>>8)&0xff);
|
||||
data[2] = (uint8_t) ((sizeoffile>>16)&0xff);
|
||||
data[3] = (uint8_t) ((sizeoffile>>24)&0xff);
|
||||
}
|
||||
|
||||
/* Close file */
|
||||
fresult = f_close(&fil);
|
||||
if (fresult != FR_OK)
|
||||
{
|
||||
char *buf = malloc(100*sizeof(char));
|
||||
sprintf (buf, "ERROR!!! No. %d in closing file *%s*\n\n", fresult, name);
|
||||
//Send_Uart(buf);
|
||||
free(buf);
|
||||
}
|
||||
else
|
||||
{
|
||||
char *buf = malloc(100*sizeof(char));
|
||||
sprintf (buf, "File *%s* CLOSED successfully\n", name);
|
||||
//Send_Uart(buf);
|
||||
free(buf);
|
||||
}
|
||||
}
|
||||
return fresult;
|
||||
}
|
||||
}
|
||||
|
||||
FRESULT Create_File (char *name)
|
||||
{
|
||||
fresult = f_stat (name, &fno);
|
||||
if (fresult == FR_OK)
|
||||
{
|
||||
char *buf = malloc(100*sizeof(char));
|
||||
//sprintf (buf, "ERROR!!! *%s* already exists!!!!\n use Update_File \n\n",name);
|
||||
//Send_Uart(buf);
|
||||
free(buf);
|
||||
return fresult;
|
||||
}
|
||||
else
|
||||
{
|
||||
fresult = f_open(&fil, name, FA_CREATE_ALWAYS|FA_READ|FA_WRITE);
|
||||
if (fresult != FR_OK)
|
||||
{
|
||||
char *buf = malloc(100*sizeof(char));
|
||||
//sprintf (buf, "ERROR!!! No. %d in creating file *%s*\n\n", fresult, name);
|
||||
//Send_Uart(buf);
|
||||
free(buf);
|
||||
return fresult;
|
||||
}
|
||||
else
|
||||
{
|
||||
char *buf = malloc(100*sizeof(char));
|
||||
//sprintf (buf, "*%s* created successfully\n Now use Write_File to write data\n",name);
|
||||
//Send_Uart(buf);
|
||||
free(buf);
|
||||
}
|
||||
|
||||
fresult = f_close(&fil);
|
||||
if (fresult != FR_OK)
|
||||
{
|
||||
char *buf = malloc(100*sizeof(char));
|
||||
//sprintf (buf, "ERROR No. %d in closing file *%s*\n\n", fresult, name);
|
||||
//Send_Uart(buf);
|
||||
free(buf);
|
||||
}
|
||||
else
|
||||
{
|
||||
char *buf = malloc(100*sizeof(char));
|
||||
//sprintf (buf, "File *%s* CLOSED successfully\n", name);
|
||||
//Send_Uart(buf);
|
||||
free(buf);
|
||||
}
|
||||
}
|
||||
return fresult;
|
||||
}
|
||||
|
||||
FRESULT Update_File (char *name, char *data)
|
||||
{
|
||||
/**** check whether the file exists or not ****/
|
||||
fresult = f_stat (name, &fno);
|
||||
if (fresult != FR_OK)
|
||||
{
|
||||
char *buf = malloc(100*sizeof(char));
|
||||
//sprintf (buf, "ERROR!!! *%s* does not exists\n\n", name);
|
||||
//Send_Uart (buf);
|
||||
free(buf);
|
||||
return fresult;
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
/* Create a file with read write access and open it */
|
||||
fresult = f_open(&fil, name, FA_OPEN_APPEND | FA_WRITE);
|
||||
if (fresult != FR_OK)
|
||||
{
|
||||
char *buf = malloc(100*sizeof(char));
|
||||
//sprintf (buf, "ERROR!!! No. %d in opening file *%s*\n\n", fresult, name);
|
||||
//Send_Uart(buf);
|
||||
free(buf);
|
||||
return fresult;
|
||||
}
|
||||
|
||||
/* Writing text */
|
||||
fresult = f_write(&fil, data, strlen (data), &bw);
|
||||
if (fresult != FR_OK)
|
||||
{
|
||||
char *buf = malloc(100*sizeof(char));
|
||||
//sprintf (buf, "ERROR!!! No. %d in writing file *%s*\n\n", fresult, name);
|
||||
//Send_Uart(buf);
|
||||
free(buf);
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
char *buf = malloc(100*sizeof(char));
|
||||
//sprintf (buf, "*%s* UPDATED successfully\n", name);
|
||||
//Send_Uart(buf);
|
||||
free(buf);
|
||||
}
|
||||
|
||||
/* Close file */
|
||||
fresult = f_close(&fil);
|
||||
if (fresult != FR_OK)
|
||||
{
|
||||
char *buf = malloc(100*sizeof(char));
|
||||
//sprintf (buf, "ERROR!!! No. %d in closing file *%s*\n\n", fresult, name);
|
||||
//Send_Uart(buf);
|
||||
free(buf);
|
||||
}
|
||||
else
|
||||
{
|
||||
char *buf = malloc(100*sizeof(char));
|
||||
//sprintf (buf, "File *%s* CLOSED successfully\n", name);
|
||||
//Send_Uart(buf);
|
||||
free(buf);
|
||||
}
|
||||
}
|
||||
return fresult;
|
||||
}
|
||||
|
||||
FRESULT Remove_File (char *name)
|
||||
{
|
||||
/**** check whether the file exists or not ****/
|
||||
fresult = f_stat (name, &fno);
|
||||
if (fresult != FR_OK)
|
||||
{
|
||||
char *buf = malloc(100*sizeof(char));
|
||||
sprintf (buf, "ERROR!!! *%s* does not exists\n\n", name);
|
||||
Send_Uart (buf);
|
||||
free(buf);
|
||||
return fresult;
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
fresult = f_unlink (name);
|
||||
if (fresult == FR_OK)
|
||||
{
|
||||
char *buf = malloc(100*sizeof(char));
|
||||
sprintf (buf, "*%s* has been removed successfully\n", name);
|
||||
Send_Uart (buf);
|
||||
free(buf);
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
char *buf = malloc(100*sizeof(char));
|
||||
sprintf (buf, "ERROR No. %d in removing *%s*\n\n",fresult, name);
|
||||
Send_Uart (buf);
|
||||
free(buf);
|
||||
}
|
||||
}
|
||||
return fresult;
|
||||
}
|
||||
|
||||
FRESULT Create_Dir (char *name)
|
||||
{
|
||||
fresult = f_mkdir(name);
|
||||
if (fresult == FR_OK)
|
||||
{
|
||||
char *buf = malloc(100*sizeof(char));
|
||||
sprintf (buf, "*%s* has been created successfully\n", name);
|
||||
Send_Uart (buf);
|
||||
free(buf);
|
||||
}
|
||||
else
|
||||
{
|
||||
char *buf = malloc(100*sizeof(char));
|
||||
sprintf (buf, "ERROR No. %d in creating directory *%s*\n\n", fresult,name);
|
||||
Send_Uart(buf);
|
||||
free(buf);
|
||||
}
|
||||
return fresult;
|
||||
}
|
||||
|
||||
void Check_SD_Space (void)
|
||||
{
|
||||
/* Check free space */
|
||||
f_getfree("", &fre_clust, &pfs);
|
||||
|
||||
total = (uint32_t)((pfs->n_fatent - 2) * pfs->csize * 0.5);
|
||||
char *buf = malloc(30*sizeof(char));
|
||||
sprintf (buf, "SD CARD Total Size: \t%lu\n",total);
|
||||
Send_Uart(buf);
|
||||
free(buf);
|
||||
free_space = (uint32_t)(fre_clust * pfs->csize * 0.5);
|
||||
buf = malloc(30*sizeof(char));
|
||||
sprintf (buf, "SD CARD Free Space: \t%lu\n",free_space);
|
||||
Send_Uart(buf);
|
||||
free(buf);
|
||||
}
|
||||
|
||||
FRESULT Update_File_float (char *name, float *data, unsigned int bytesize)
|
||||
{
|
||||
/**** check whether the file exists or not ****/
|
||||
fresult = f_stat (name, &fno);
|
||||
if (fresult != FR_OK)
|
||||
{
|
||||
char *buf = malloc(100*sizeof(char));
|
||||
//sprintf (buf, "ERROR!!! *%s* does not exists\n\n", name);
|
||||
//Send_Uart (buf);
|
||||
free(buf);
|
||||
return fresult;
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
/* Create a file with read write access and open it */
|
||||
fresult = f_open(&fil, name, FA_OPEN_APPEND | FA_WRITE);
|
||||
if (fresult != FR_OK)
|
||||
{
|
||||
char *buf = malloc(100*sizeof(char));
|
||||
//sprintf (buf, "ERROR!!! No. %d in opening file *%s*\n\n", fresult, name);
|
||||
//Send_Uart(buf);
|
||||
free(buf);
|
||||
return fresult;
|
||||
}
|
||||
|
||||
/* Writing text */
|
||||
fresult = f_write(&fil, data, bytesize, &bw);
|
||||
if (fresult != FR_OK)
|
||||
{
|
||||
char *buf = malloc(100*sizeof(char));
|
||||
//sprintf (buf, "ERROR!!! No. %d in writing file *%s*\n\n", fresult, name);
|
||||
//Send_Uart(buf);
|
||||
free(buf);
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
char *buf = malloc(100*sizeof(char));
|
||||
//sprintf (buf, "*%s* UPDATED successfully\n", name);
|
||||
//Send_Uart(buf);
|
||||
free(buf);
|
||||
}
|
||||
|
||||
/* Close file */
|
||||
fresult = f_close(&fil);
|
||||
if (fresult != FR_OK)
|
||||
{
|
||||
char *buf = malloc(100*sizeof(char));
|
||||
//sprintf (buf, "ERROR!!! No. %d in closing file *%s*\n\n", fresult, name);
|
||||
//Send_Uart(buf);
|
||||
free(buf);
|
||||
}
|
||||
else
|
||||
{
|
||||
char *buf = malloc(100*sizeof(char));
|
||||
//sprintf (buf, "File *%s* CLOSED successfully\n", name);
|
||||
//Send_Uart(buf);
|
||||
free(buf);
|
||||
}
|
||||
}
|
||||
return fresult;
|
||||
}
|
||||
|
||||
FRESULT Update_File_byte (char *name, uint8_t *data, unsigned int bytesize)
|
||||
{
|
||||
/**** check whether the file exists or not ****/
|
||||
fresult = f_stat (name, &fno);
|
||||
if (fresult != FR_OK)
|
||||
{
|
||||
char *buf = malloc(100*sizeof(char));
|
||||
//sprintf (buf, "ERROR!!! *%s* does not exists\n\n", name);
|
||||
//Send_Uart (buf);
|
||||
free(buf);
|
||||
return fresult;
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
/* Create a file with read write access and open it */
|
||||
fresult = f_open(&fil, name, FA_OPEN_APPEND | FA_WRITE);
|
||||
if (fresult != FR_OK)
|
||||
{
|
||||
char *buf = malloc(100*sizeof(char));
|
||||
//sprintf (buf, "ERROR!!! No. %d in opening file *%s*\n\n", fresult, name);
|
||||
//Send_Uart(buf);
|
||||
free(buf);
|
||||
return fresult;
|
||||
}
|
||||
|
||||
/* Writing text */
|
||||
fresult = f_write(&fil, data, bytesize, &bw);
|
||||
if (fresult != FR_OK)
|
||||
{
|
||||
char *buf = malloc(100*sizeof(char));
|
||||
//sprintf (buf, "ERROR!!! No. %d in writing file *%s*\n\n", fresult, name);
|
||||
//Send_Uart(buf);
|
||||
free(buf);
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
char *buf = malloc(100*sizeof(char));
|
||||
//sprintf (buf, "*%s* UPDATED successfully\n", name);
|
||||
//Send_Uart(buf);
|
||||
free(buf);
|
||||
}
|
||||
|
||||
/* Close file */
|
||||
fresult = f_close(&fil);
|
||||
if (fresult != FR_OK)
|
||||
{
|
||||
char *buf = malloc(100*sizeof(char));
|
||||
//sprintf (buf, "ERROR!!! No. %d in closing file *%s*\n\n", fresult, name);
|
||||
//Send_Uart(buf);
|
||||
free(buf);
|
||||
}
|
||||
else
|
||||
{
|
||||
char *buf = malloc(100*sizeof(char));
|
||||
//sprintf (buf, "File *%s* CLOSED successfully\n", name);
|
||||
//Send_Uart(buf);
|
||||
free(buf);
|
||||
}
|
||||
}
|
||||
return fresult;
|
||||
}
|
||||
314
Src/bsp_driver_sd.c
Normal file
314
Src/bsp_driver_sd.c
Normal file
@ -0,0 +1,314 @@
|
||||
/* USER CODE BEGIN Header */
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file bsp_driver_sd.c for F7 (based on stm32756g_eval_sd.c)
|
||||
* @brief This file includes a generic uSD card driver.
|
||||
* To be completed by the user according to the board used for the project.
|
||||
* @note Some functions generated as weak: they can be overridden by
|
||||
* - code in user files
|
||||
* - or BSP code from the FW pack files
|
||||
* if such files are added to the generated project (by the user).
|
||||
******************************************************************************
|
||||
* @attention
|
||||
*
|
||||
* Copyright (c) 2023 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 */
|
||||
|
||||
#ifdef OLD_API
|
||||
/* kept to avoid issue when migrating old projects. */
|
||||
/* USER CODE BEGIN 0 */
|
||||
|
||||
/* USER CODE END 0 */
|
||||
#else
|
||||
/* USER CODE BEGIN FirstSection */
|
||||
/* can be used to modify / undefine following code or add new definitions */
|
||||
/* USER CODE END FirstSection */
|
||||
/* Includes ------------------------------------------------------------------*/
|
||||
#include "bsp_driver_sd.h"
|
||||
|
||||
/* Extern variables ---------------------------------------------------------*/
|
||||
|
||||
extern SD_HandleTypeDef hsd1;
|
||||
|
||||
/* USER CODE BEGIN BeforeInitSection */
|
||||
/* can be used to modify / undefine following code or add code */
|
||||
/* USER CODE END BeforeInitSection */
|
||||
/**
|
||||
* @brief Initializes the SD card device.
|
||||
* @retval SD status
|
||||
*/
|
||||
__weak uint8_t BSP_SD_Init(void)
|
||||
{
|
||||
uint8_t sd_state = MSD_OK;
|
||||
/* Check if the SD card is plugged in the slot */
|
||||
if (BSP_SD_IsDetected() != SD_PRESENT)
|
||||
{
|
||||
return MSD_ERROR_SD_NOT_PRESENT;
|
||||
}
|
||||
/* HAL SD initialization */
|
||||
sd_state = HAL_SD_Init(&hsd1);
|
||||
/* Configure SD Bus width (4 bits mode selected) */
|
||||
if (sd_state == MSD_OK)
|
||||
{
|
||||
/* Enable wide operation */
|
||||
if (HAL_SD_ConfigWideBusOperation(&hsd1, SDMMC_BUS_WIDE_4B) != HAL_OK)
|
||||
{
|
||||
sd_state = MSD_ERROR;
|
||||
}
|
||||
}
|
||||
|
||||
return sd_state;
|
||||
}
|
||||
/* USER CODE BEGIN AfterInitSection */
|
||||
/* can be used to modify previous code / undefine following code / add code */
|
||||
/* USER CODE END AfterInitSection */
|
||||
|
||||
/* USER CODE BEGIN InterruptMode */
|
||||
/**
|
||||
* @brief Configures Interrupt mode for SD detection pin.
|
||||
* @retval Returns 0
|
||||
*/
|
||||
__weak uint8_t BSP_SD_ITConfig(void)
|
||||
{
|
||||
/* Code to be updated by the user or replaced by one from the FW pack (in a stmxxxx_sd.c file) */
|
||||
|
||||
return (uint8_t)0;
|
||||
}
|
||||
|
||||
/* USER CODE END InterruptMode */
|
||||
|
||||
/* USER CODE BEGIN BeforeReadBlocksSection */
|
||||
/* can be used to modify previous code / undefine following code / add code */
|
||||
/* USER CODE END BeforeReadBlocksSection */
|
||||
/**
|
||||
* @brief Reads block(s) from a specified address in an SD card, in polling mode.
|
||||
* @param pData: Pointer to the buffer that will contain the data to transmit
|
||||
* @param ReadAddr: Address from where data is to be read
|
||||
* @param NumOfBlocks: Number of SD blocks to read
|
||||
* @param Timeout: Timeout for read operation
|
||||
* @retval SD status
|
||||
*/
|
||||
__weak uint8_t BSP_SD_ReadBlocks(uint32_t *pData, uint32_t ReadAddr, uint32_t NumOfBlocks, uint32_t Timeout)
|
||||
{
|
||||
uint8_t sd_state = MSD_OK;
|
||||
|
||||
if (HAL_SD_ReadBlocks(&hsd1, (uint8_t *)pData, ReadAddr, NumOfBlocks, Timeout) != HAL_OK)
|
||||
{
|
||||
sd_state = MSD_ERROR;
|
||||
}
|
||||
|
||||
return sd_state;
|
||||
}
|
||||
|
||||
/* USER CODE BEGIN BeforeWriteBlocksSection */
|
||||
/* can be used to modify previous code / undefine following code / add code */
|
||||
/* USER CODE END BeforeWriteBlocksSection */
|
||||
/**
|
||||
* @brief Writes block(s) to a specified address in an SD card, in polling mode.
|
||||
* @param pData: Pointer to the buffer that will contain the data to transmit
|
||||
* @param WriteAddr: Address from where data is to be written
|
||||
* @param NumOfBlocks: Number of SD blocks to write
|
||||
* @param Timeout: Timeout for write operation
|
||||
* @retval SD status
|
||||
*/
|
||||
__weak uint8_t BSP_SD_WriteBlocks(uint32_t *pData, uint32_t WriteAddr, uint32_t NumOfBlocks, uint32_t Timeout)
|
||||
{
|
||||
uint8_t sd_state = MSD_OK;
|
||||
|
||||
if (HAL_SD_WriteBlocks(&hsd1, (uint8_t *)pData, WriteAddr, NumOfBlocks, Timeout) != HAL_OK)
|
||||
{
|
||||
sd_state = MSD_ERROR;
|
||||
}
|
||||
|
||||
return sd_state;
|
||||
}
|
||||
|
||||
/* USER CODE BEGIN BeforeReadDMABlocksSection */
|
||||
/* can be used to modify previous code / undefine following code / add code */
|
||||
/* USER CODE END BeforeReadDMABlocksSection */
|
||||
/**
|
||||
* @brief Reads block(s) from a specified address in an SD card, in DMA mode.
|
||||
* @param pData: Pointer to the buffer that will contain the data to transmit
|
||||
* @param ReadAddr: Address from where data is to be read
|
||||
* @param NumOfBlocks: Number of SD blocks to read
|
||||
* @retval SD status
|
||||
*/
|
||||
__weak uint8_t BSP_SD_ReadBlocks_DMA(uint32_t *pData, uint32_t ReadAddr, uint32_t NumOfBlocks)
|
||||
{
|
||||
uint8_t sd_state = MSD_OK;
|
||||
|
||||
/* Read block(s) in DMA transfer mode */
|
||||
if (HAL_SD_ReadBlocks_DMA(&hsd1, (uint8_t *)pData, ReadAddr, NumOfBlocks) != HAL_OK)
|
||||
{
|
||||
sd_state = MSD_ERROR;
|
||||
}
|
||||
|
||||
return sd_state;
|
||||
}
|
||||
|
||||
/* USER CODE BEGIN BeforeWriteDMABlocksSection */
|
||||
/* can be used to modify previous code / undefine following code / add code */
|
||||
/* USER CODE END BeforeWriteDMABlocksSection */
|
||||
/**
|
||||
* @brief Writes block(s) to a specified address in an SD card, in DMA mode.
|
||||
* @param pData: Pointer to the buffer that will contain the data to transmit
|
||||
* @param WriteAddr: Address from where data is to be written
|
||||
* @param NumOfBlocks: Number of SD blocks to write
|
||||
* @retval SD status
|
||||
*/
|
||||
__weak uint8_t BSP_SD_WriteBlocks_DMA(uint32_t *pData, uint32_t WriteAddr, uint32_t NumOfBlocks)
|
||||
{
|
||||
uint8_t sd_state = MSD_OK;
|
||||
|
||||
/* Write block(s) in DMA transfer mode */
|
||||
if (HAL_SD_WriteBlocks_DMA(&hsd1, (uint8_t *)pData, WriteAddr, NumOfBlocks) != HAL_OK)
|
||||
{
|
||||
sd_state = MSD_ERROR;
|
||||
}
|
||||
|
||||
return sd_state;
|
||||
}
|
||||
|
||||
/* USER CODE BEGIN BeforeEraseSection */
|
||||
/* can be used to modify previous code / undefine following code / add code */
|
||||
/* USER CODE END BeforeEraseSection */
|
||||
/**
|
||||
* @brief Erases the specified memory area of the given SD card.
|
||||
* @param StartAddr: Start byte address
|
||||
* @param EndAddr: End byte address
|
||||
* @retval SD status
|
||||
*/
|
||||
__weak uint8_t BSP_SD_Erase(uint32_t StartAddr, uint32_t EndAddr)
|
||||
{
|
||||
uint8_t sd_state = MSD_OK;
|
||||
|
||||
if (HAL_SD_Erase(&hsd1, StartAddr, EndAddr) != HAL_OK)
|
||||
{
|
||||
sd_state = MSD_ERROR;
|
||||
}
|
||||
|
||||
return sd_state;
|
||||
}
|
||||
|
||||
/* USER CODE BEGIN BeforeGetCardStateSection */
|
||||
/* can be used to modify previous code / undefine following code / add code */
|
||||
/* USER CODE END BeforeGetCardStateSection */
|
||||
|
||||
/**
|
||||
* @brief Gets the current SD card data status.
|
||||
* @param None
|
||||
* @retval Data transfer state.
|
||||
* This value can be one of the following values:
|
||||
* @arg SD_TRANSFER_OK: No data transfer is acting
|
||||
* @arg SD_TRANSFER_BUSY: Data transfer is acting
|
||||
*/
|
||||
__weak uint8_t BSP_SD_GetCardState(void)
|
||||
{
|
||||
return ((HAL_SD_GetCardState(&hsd1) == HAL_SD_CARD_TRANSFER ) ? SD_TRANSFER_OK : SD_TRANSFER_BUSY);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Get SD information about specific SD card.
|
||||
* @param CardInfo: Pointer to HAL_SD_CardInfoTypedef structure
|
||||
* @retval None
|
||||
*/
|
||||
__weak void BSP_SD_GetCardInfo(HAL_SD_CardInfoTypeDef *CardInfo)
|
||||
{
|
||||
/* Get SD card Information */
|
||||
HAL_SD_GetCardInfo(&hsd1, CardInfo);
|
||||
}
|
||||
|
||||
/* USER CODE BEGIN BeforeCallBacksSection */
|
||||
/* can be used to modify previous code / undefine following code / add code */
|
||||
/* USER CODE END BeforeCallBacksSection */
|
||||
/**
|
||||
* @brief SD Abort callbacks
|
||||
* @param hsd: SD handle
|
||||
* @retval None
|
||||
*/
|
||||
void HAL_SD_AbortCallback(SD_HandleTypeDef *hsd)
|
||||
{
|
||||
BSP_SD_AbortCallback();
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Tx Transfer completed callback
|
||||
* @param hsd: SD handle
|
||||
* @retval None
|
||||
*/
|
||||
void HAL_SD_TxCpltCallback(SD_HandleTypeDef *hsd)
|
||||
{
|
||||
BSP_SD_WriteCpltCallback();
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Rx Transfer completed callback
|
||||
* @param hsd: SD handle
|
||||
* @retval None
|
||||
*/
|
||||
void HAL_SD_RxCpltCallback(SD_HandleTypeDef *hsd)
|
||||
{
|
||||
BSP_SD_ReadCpltCallback();
|
||||
}
|
||||
|
||||
/* USER CODE BEGIN CallBacksSection_C */
|
||||
/**
|
||||
* @brief BSP SD Abort callback
|
||||
* @retval None
|
||||
* @note empty (up to the user to fill it in or to remove it if useless)
|
||||
*/
|
||||
__weak void BSP_SD_AbortCallback(void)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief BSP Tx Transfer completed callback
|
||||
* @retval None
|
||||
* @note empty (up to the user to fill it in or to remove it if useless)
|
||||
*/
|
||||
__weak void BSP_SD_WriteCpltCallback(void)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief BSP Rx Transfer completed callback
|
||||
* @retval None
|
||||
* @note empty (up to the user to fill it in or to remove it if useless)
|
||||
*/
|
||||
__weak void BSP_SD_ReadCpltCallback(void)
|
||||
{
|
||||
|
||||
}
|
||||
/* USER CODE END CallBacksSection_C */
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @brief Detects if SD card is correctly plugged in the memory slot or not.
|
||||
* @param None
|
||||
* @retval Returns if SD is detected or not
|
||||
*/
|
||||
__weak uint8_t BSP_SD_IsDetected(void)
|
||||
{
|
||||
__IO uint8_t status = SD_PRESENT;
|
||||
|
||||
if (BSP_PlatformIsDetected() == 0x0)
|
||||
{
|
||||
status = SD_NOT_PRESENT;
|
||||
}
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
/* USER CODE BEGIN AdditionalCode */
|
||||
/* user code can be inserted here */
|
||||
/* USER CODE END AdditionalCode */
|
||||
54
Src/fatfs.c
Normal file
54
Src/fatfs.c
Normal file
@ -0,0 +1,54 @@
|
||||
/* USER CODE BEGIN Header */
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file fatfs.c
|
||||
* @brief Code for fatfs applications
|
||||
******************************************************************************
|
||||
* @attention
|
||||
*
|
||||
* Copyright (c) 2023 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 */
|
||||
#include "fatfs.h"
|
||||
|
||||
uint8_t retSD; /* Return value for SD */
|
||||
char SDPath[4]; /* SD logical drive path */
|
||||
FATFS SDFatFS; /* File system object for SD logical drive */
|
||||
FIL SDFile; /* File object for SD */
|
||||
|
||||
/* USER CODE BEGIN Variables */
|
||||
|
||||
/* USER CODE END Variables */
|
||||
|
||||
void MX_FATFS_Init(void)
|
||||
{
|
||||
/*## FatFS: Link the SD driver ###########################*/
|
||||
retSD = FATFS_LinkDriver(&SD_Driver, SDPath);
|
||||
|
||||
/* USER CODE BEGIN Init */
|
||||
/* additional user code for init */
|
||||
/* USER CODE END Init */
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Gets Time from RTC
|
||||
* @param None
|
||||
* @retval Time in DWORD
|
||||
*/
|
||||
DWORD get_fattime(void)
|
||||
{
|
||||
/* USER CODE BEGIN get_fattime */
|
||||
return 0;
|
||||
/* USER CODE END get_fattime */
|
||||
}
|
||||
|
||||
/* USER CODE BEGIN Application */
|
||||
|
||||
/* USER CODE END Application */
|
||||
32
Src/fatfs_platform.c
Normal file
32
Src/fatfs_platform.c
Normal file
@ -0,0 +1,32 @@
|
||||
/* USER CODE BEGIN Header */
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file : fatfs_platform.c
|
||||
* @brief : fatfs_platform source file
|
||||
******************************************************************************
|
||||
* @attention
|
||||
*
|
||||
* Copyright (c) 2023 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 */
|
||||
#include "fatfs_platform.h"
|
||||
|
||||
uint8_t BSP_PlatformIsDetected(void) {
|
||||
uint8_t status = SD_PRESENT;
|
||||
/* Check SD card detect pin */
|
||||
if(HAL_GPIO_ReadPin(SD_DETECT_GPIO_PORT, SD_DETECT_PIN) != GPIO_PIN_RESET)
|
||||
{
|
||||
status = SD_NOT_PRESENT;
|
||||
}
|
||||
/* USER CODE BEGIN 1 */
|
||||
/* user code can be inserted here */
|
||||
/* USER CODE END 1 */
|
||||
return status;
|
||||
}
|
||||
2244
Src/main.c
Normal file
2244
Src/main.c
Normal file
File diff suppressed because it is too large
Load Diff
257
Src/sd_diskio.c
Normal file
257
Src/sd_diskio.c
Normal file
@ -0,0 +1,257 @@
|
||||
/* USER CODE BEGIN Header */
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file sd_diskio.c
|
||||
* @brief SD Disk I/O driver
|
||||
******************************************************************************
|
||||
* @attention
|
||||
*
|
||||
* Copyright (c) 2023 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 */
|
||||
|
||||
/* Note: code generation based on sd_diskio_template_bspv1.c v2.1.4
|
||||
as "Use dma template" is disabled. */
|
||||
|
||||
/* USER CODE BEGIN firstSection */
|
||||
/* can be used to modify / undefine following code or add new definitions */
|
||||
/* USER CODE END firstSection*/
|
||||
|
||||
/* Includes ------------------------------------------------------------------*/
|
||||
#include "ff_gen_drv.h"
|
||||
#include "sd_diskio.h"
|
||||
|
||||
/* Private typedef -----------------------------------------------------------*/
|
||||
/* Private define ------------------------------------------------------------*/
|
||||
/* use the default SD timout as defined in the platform BSP driver*/
|
||||
#if defined(SDMMC_DATATIMEOUT)
|
||||
#define SD_TIMEOUT SDMMC_DATATIMEOUT
|
||||
#elif defined(SD_DATATIMEOUT)
|
||||
#define SD_TIMEOUT SD_DATATIMEOUT
|
||||
#else
|
||||
#define SD_TIMEOUT 30 * 1000
|
||||
#endif
|
||||
|
||||
#define SD_DEFAULT_BLOCK_SIZE 512
|
||||
|
||||
/*
|
||||
* Depending on the use case, the SD card initialization could be done at the
|
||||
* application level: if it is the case define the flag below to disable
|
||||
* the BSP_SD_Init() call in the SD_Initialize() and add a call to
|
||||
* BSP_SD_Init() elsewhere in the application.
|
||||
*/
|
||||
/* USER CODE BEGIN disableSDInit */
|
||||
/* #define DISABLE_SD_INIT */
|
||||
/* USER CODE END disableSDInit */
|
||||
|
||||
/* Private variables ---------------------------------------------------------*/
|
||||
/* Disk status */
|
||||
static volatile DSTATUS Stat = STA_NOINIT;
|
||||
|
||||
/* Private function prototypes -----------------------------------------------*/
|
||||
static DSTATUS SD_CheckStatus(BYTE lun);
|
||||
DSTATUS SD_initialize (BYTE);
|
||||
DSTATUS SD_status (BYTE);
|
||||
DRESULT SD_read (BYTE, BYTE*, DWORD, UINT);
|
||||
#if _USE_WRITE == 1
|
||||
DRESULT SD_write (BYTE, const BYTE*, DWORD, UINT);
|
||||
#endif /* _USE_WRITE == 1 */
|
||||
#if _USE_IOCTL == 1
|
||||
DRESULT SD_ioctl (BYTE, BYTE, void*);
|
||||
#endif /* _USE_IOCTL == 1 */
|
||||
|
||||
const Diskio_drvTypeDef SD_Driver =
|
||||
{
|
||||
SD_initialize,
|
||||
SD_status,
|
||||
SD_read,
|
||||
#if _USE_WRITE == 1
|
||||
SD_write,
|
||||
#endif /* _USE_WRITE == 1 */
|
||||
|
||||
#if _USE_IOCTL == 1
|
||||
SD_ioctl,
|
||||
#endif /* _USE_IOCTL == 1 */
|
||||
};
|
||||
|
||||
/* USER CODE BEGIN beforeFunctionSection */
|
||||
/* can be used to modify / undefine following code or add new code */
|
||||
/* USER CODE END beforeFunctionSection */
|
||||
|
||||
/* Private functions ---------------------------------------------------------*/
|
||||
|
||||
static DSTATUS SD_CheckStatus(BYTE lun)
|
||||
{
|
||||
Stat = STA_NOINIT;
|
||||
|
||||
if(BSP_SD_GetCardState() == MSD_OK)
|
||||
{
|
||||
Stat &= ~STA_NOINIT;
|
||||
}
|
||||
|
||||
return Stat;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Initializes a Drive
|
||||
* @param lun : not used
|
||||
* @retval DSTATUS: Operation status
|
||||
*/
|
||||
DSTATUS SD_initialize(BYTE lun)
|
||||
{
|
||||
Stat = STA_NOINIT;
|
||||
|
||||
#if !defined(DISABLE_SD_INIT)
|
||||
|
||||
if(BSP_SD_Init() == MSD_OK)
|
||||
{
|
||||
Stat = SD_CheckStatus(lun);
|
||||
}
|
||||
|
||||
#else
|
||||
Stat = SD_CheckStatus(lun);
|
||||
#endif
|
||||
|
||||
return Stat;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Gets Disk Status
|
||||
* @param lun : not used
|
||||
* @retval DSTATUS: Operation status
|
||||
*/
|
||||
DSTATUS SD_status(BYTE lun)
|
||||
{
|
||||
return SD_CheckStatus(lun);
|
||||
}
|
||||
|
||||
/* USER CODE BEGIN beforeReadSection */
|
||||
/* can be used to modify previous code / undefine following code / add new code */
|
||||
/* USER CODE END beforeReadSection */
|
||||
/**
|
||||
* @brief Reads Sector(s)
|
||||
* @param lun : not used
|
||||
* @param *buff: Data buffer to store read data
|
||||
* @param sector: Sector address (LBA)
|
||||
* @param count: Number of sectors to read (1..128)
|
||||
* @retval DRESULT: Operation result
|
||||
*/
|
||||
|
||||
DRESULT SD_read(BYTE lun, BYTE *buff, DWORD sector, UINT count)
|
||||
{
|
||||
DRESULT res = RES_ERROR;
|
||||
|
||||
if(BSP_SD_ReadBlocks((uint32_t*)buff,
|
||||
(uint32_t) (sector),
|
||||
count, SD_TIMEOUT) == MSD_OK)
|
||||
{
|
||||
/* wait until the read operation is finished */
|
||||
while(BSP_SD_GetCardState()!= MSD_OK)
|
||||
{
|
||||
}
|
||||
res = RES_OK;
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
/* USER CODE BEGIN beforeWriteSection */
|
||||
/* can be used to modify previous code / undefine following code / add new code */
|
||||
/* USER CODE END beforeWriteSection */
|
||||
/**
|
||||
* @brief Writes Sector(s)
|
||||
* @param lun : not used
|
||||
* @param *buff: Data to be written
|
||||
* @param sector: Sector address (LBA)
|
||||
* @param count: Number of sectors to write (1..128)
|
||||
* @retval DRESULT: Operation result
|
||||
*/
|
||||
#if _USE_WRITE == 1
|
||||
|
||||
DRESULT SD_write(BYTE lun, const BYTE *buff, DWORD sector, UINT count)
|
||||
{
|
||||
DRESULT res = RES_ERROR;
|
||||
|
||||
if(BSP_SD_WriteBlocks((uint32_t*)buff,
|
||||
(uint32_t)(sector),
|
||||
count, SD_TIMEOUT) == MSD_OK)
|
||||
{
|
||||
/* wait until the Write operation is finished */
|
||||
while(BSP_SD_GetCardState() != MSD_OK)
|
||||
{
|
||||
}
|
||||
res = RES_OK;
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
#endif /* _USE_WRITE == 1 */
|
||||
|
||||
/* USER CODE BEGIN beforeIoctlSection */
|
||||
/* can be used to modify previous code / undefine following code / add new code */
|
||||
/* USER CODE END beforeIoctlSection */
|
||||
/**
|
||||
* @brief I/O control operation
|
||||
* @param lun : not used
|
||||
* @param cmd: Control code
|
||||
* @param *buff: Buffer to send/receive control data
|
||||
* @retval DRESULT: Operation result
|
||||
*/
|
||||
#if _USE_IOCTL == 1
|
||||
DRESULT SD_ioctl(BYTE lun, BYTE cmd, void *buff)
|
||||
{
|
||||
DRESULT res = RES_ERROR;
|
||||
BSP_SD_CardInfo CardInfo;
|
||||
|
||||
if (Stat & STA_NOINIT) return RES_NOTRDY;
|
||||
|
||||
switch (cmd)
|
||||
{
|
||||
/* Make sure that no pending write process */
|
||||
case CTRL_SYNC :
|
||||
res = RES_OK;
|
||||
break;
|
||||
|
||||
/* Get number of sectors on the disk (DWORD) */
|
||||
case GET_SECTOR_COUNT :
|
||||
BSP_SD_GetCardInfo(&CardInfo);
|
||||
*(DWORD*)buff = CardInfo.LogBlockNbr;
|
||||
res = RES_OK;
|
||||
break;
|
||||
|
||||
/* Get R/W sector size (WORD) */
|
||||
case GET_SECTOR_SIZE :
|
||||
BSP_SD_GetCardInfo(&CardInfo);
|
||||
*(WORD*)buff = CardInfo.LogBlockSize;
|
||||
res = RES_OK;
|
||||
break;
|
||||
|
||||
/* Get erase block size in unit of sector (DWORD) */
|
||||
case GET_BLOCK_SIZE :
|
||||
BSP_SD_GetCardInfo(&CardInfo);
|
||||
*(DWORD*)buff = CardInfo.LogBlockSize / SD_DEFAULT_BLOCK_SIZE;
|
||||
res = RES_OK;
|
||||
break;
|
||||
|
||||
default:
|
||||
res = RES_PARERR;
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
#endif /* _USE_IOCTL == 1 */
|
||||
|
||||
/* USER CODE BEGIN afterIoctlSection */
|
||||
/* can be used to modify previous code / undefine following code / add new code */
|
||||
/* USER CODE END afterIoctlSection */
|
||||
|
||||
/* USER CODE BEGIN lastSection */
|
||||
/* can be used to modify / undefine previous code or add new code */
|
||||
/* USER CODE END lastSection */
|
||||
376
Src/stm32f7xx_hal_msp.c
Normal file
376
Src/stm32f7xx_hal_msp.c
Normal file
@ -0,0 +1,376 @@
|
||||
/* USER CODE BEGIN Header */
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file stm32f7xx_hal_msp.c
|
||||
* @brief This file provides code for the MSP Initialization
|
||||
* and de-Initialization codes.
|
||||
******************************************************************************
|
||||
* @attention
|
||||
*
|
||||
* Copyright (c) 2023 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"
|
||||
|
||||
/* USER CODE BEGIN Includes */
|
||||
|
||||
/* USER CODE END Includes */
|
||||
|
||||
/* Private typedef -----------------------------------------------------------*/
|
||||
/* USER CODE BEGIN TD */
|
||||
|
||||
/* USER CODE END TD */
|
||||
|
||||
/* Private define ------------------------------------------------------------*/
|
||||
/* USER CODE BEGIN Define */
|
||||
|
||||
/* USER CODE END Define */
|
||||
|
||||
/* Private macro -------------------------------------------------------------*/
|
||||
/* USER CODE BEGIN Macro */
|
||||
|
||||
/* USER CODE END Macro */
|
||||
|
||||
/* Private variables ---------------------------------------------------------*/
|
||||
/* USER CODE BEGIN PV */
|
||||
|
||||
/* USER CODE END PV */
|
||||
|
||||
/* Private function prototypes -----------------------------------------------*/
|
||||
/* USER CODE BEGIN PFP */
|
||||
|
||||
/* USER CODE END PFP */
|
||||
|
||||
/* External functions --------------------------------------------------------*/
|
||||
/* USER CODE BEGIN ExternalFunctions */
|
||||
|
||||
/* USER CODE END ExternalFunctions */
|
||||
|
||||
/* USER CODE BEGIN 0 */
|
||||
|
||||
/* USER CODE END 0 */
|
||||
/**
|
||||
* Initializes the Global MSP.
|
||||
*/
|
||||
void HAL_MspInit(void)
|
||||
{
|
||||
/* USER CODE BEGIN MspInit 0 */
|
||||
|
||||
/* USER CODE END MspInit 0 */
|
||||
|
||||
__HAL_RCC_PWR_CLK_ENABLE();
|
||||
__HAL_RCC_SYSCFG_CLK_ENABLE();
|
||||
|
||||
/* System interrupt init*/
|
||||
|
||||
/* USER CODE BEGIN MspInit 1 */
|
||||
|
||||
/* USER CODE END MspInit 1 */
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief ADC MSP Initialization
|
||||
* This function configures the hardware resources used in this example
|
||||
* @param hadc: ADC handle pointer
|
||||
* @retval None
|
||||
*/
|
||||
void HAL_ADC_MspInit(ADC_HandleTypeDef* hadc)
|
||||
{
|
||||
GPIO_InitTypeDef GPIO_InitStruct = {0};
|
||||
if(hadc->Instance==ADC1)
|
||||
{
|
||||
/* USER CODE BEGIN ADC1_MspInit 0 */
|
||||
|
||||
/* USER CODE END ADC1_MspInit 0 */
|
||||
/* Peripheral clock enable */
|
||||
__HAL_RCC_ADC1_CLK_ENABLE();
|
||||
|
||||
__HAL_RCC_GPIOC_CLK_ENABLE();
|
||||
__HAL_RCC_GPIOA_CLK_ENABLE();
|
||||
__HAL_RCC_GPIOB_CLK_ENABLE();
|
||||
/**ADC1 GPIO Configuration
|
||||
PC0 ------> ADC1_IN10
|
||||
PC1 ------> ADC1_IN11
|
||||
PA2 ------> ADC1_IN2
|
||||
PB0 ------> ADC1_IN8
|
||||
PB1 ------> ADC1_IN9
|
||||
*/
|
||||
GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
|
||||
|
||||
GPIO_InitStruct.Pin = GPIO_PIN_2;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
|
||||
|
||||
GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
|
||||
|
||||
/* ADC1 interrupt Init */
|
||||
HAL_NVIC_SetPriority(ADC_IRQn, 0, 0);
|
||||
HAL_NVIC_EnableIRQ(ADC_IRQn);
|
||||
/* USER CODE BEGIN ADC1_MspInit 1 */
|
||||
|
||||
/* USER CODE END ADC1_MspInit 1 */
|
||||
}
|
||||
else if(hadc->Instance==ADC3)
|
||||
{
|
||||
/* USER CODE BEGIN ADC3_MspInit 0 */
|
||||
|
||||
/* USER CODE END ADC3_MspInit 0 */
|
||||
/* Peripheral clock enable */
|
||||
__HAL_RCC_ADC3_CLK_ENABLE();
|
||||
|
||||
__HAL_RCC_GPIOF_CLK_ENABLE();
|
||||
/**ADC3 GPIO Configuration
|
||||
PF5 ------> ADC3_IN15
|
||||
*/
|
||||
GPIO_InitStruct.Pin = GPIO_PIN_5;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
HAL_GPIO_Init(GPIOF, &GPIO_InitStruct);
|
||||
|
||||
/* ADC3 interrupt Init */
|
||||
HAL_NVIC_SetPriority(ADC_IRQn, 0, 0);
|
||||
HAL_NVIC_EnableIRQ(ADC_IRQn);
|
||||
/* USER CODE BEGIN ADC3_MspInit 1 */
|
||||
|
||||
/* USER CODE END ADC3_MspInit 1 */
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief ADC MSP De-Initialization
|
||||
* This function freeze the hardware resources used in this example
|
||||
* @param hadc: ADC handle pointer
|
||||
* @retval None
|
||||
*/
|
||||
void HAL_ADC_MspDeInit(ADC_HandleTypeDef* hadc)
|
||||
{
|
||||
if(hadc->Instance==ADC1)
|
||||
{
|
||||
/* USER CODE BEGIN ADC1_MspDeInit 0 */
|
||||
|
||||
/* USER CODE END ADC1_MspDeInit 0 */
|
||||
/* Peripheral clock disable */
|
||||
__HAL_RCC_ADC1_CLK_DISABLE();
|
||||
|
||||
/**ADC1 GPIO Configuration
|
||||
PC0 ------> ADC1_IN10
|
||||
PC1 ------> ADC1_IN11
|
||||
PA2 ------> ADC1_IN2
|
||||
PB0 ------> ADC1_IN8
|
||||
PB1 ------> ADC1_IN9
|
||||
*/
|
||||
HAL_GPIO_DeInit(GPIOC, GPIO_PIN_0|GPIO_PIN_1);
|
||||
|
||||
HAL_GPIO_DeInit(GPIOA, GPIO_PIN_2);
|
||||
|
||||
HAL_GPIO_DeInit(GPIOB, GPIO_PIN_0|GPIO_PIN_1);
|
||||
|
||||
/* ADC1 interrupt DeInit */
|
||||
/* USER CODE BEGIN ADC1:ADC_IRQn disable */
|
||||
/**
|
||||
* Uncomment the line below to disable the "ADC_IRQn" interrupt
|
||||
* Be aware, disabling shared interrupt may affect other IPs
|
||||
*/
|
||||
/* HAL_NVIC_DisableIRQ(ADC_IRQn); */
|
||||
/* USER CODE END ADC1:ADC_IRQn disable */
|
||||
|
||||
/* USER CODE BEGIN ADC1_MspDeInit 1 */
|
||||
|
||||
/* USER CODE END ADC1_MspDeInit 1 */
|
||||
}
|
||||
else if(hadc->Instance==ADC3)
|
||||
{
|
||||
/* USER CODE BEGIN ADC3_MspDeInit 0 */
|
||||
|
||||
/* USER CODE END ADC3_MspDeInit 0 */
|
||||
/* Peripheral clock disable */
|
||||
__HAL_RCC_ADC3_CLK_DISABLE();
|
||||
|
||||
/**ADC3 GPIO Configuration
|
||||
PF5 ------> ADC3_IN15
|
||||
*/
|
||||
HAL_GPIO_DeInit(GPIOF, GPIO_PIN_5);
|
||||
|
||||
/* ADC3 interrupt DeInit */
|
||||
/* USER CODE BEGIN ADC3:ADC_IRQn disable */
|
||||
/**
|
||||
* Uncomment the line below to disable the "ADC_IRQn" interrupt
|
||||
* Be aware, disabling shared interrupt may affect other IPs
|
||||
*/
|
||||
/* HAL_NVIC_DisableIRQ(ADC_IRQn); */
|
||||
/* USER CODE END ADC3:ADC_IRQn disable */
|
||||
|
||||
/* USER CODE BEGIN ADC3_MspDeInit 1 */
|
||||
|
||||
/* USER CODE END ADC3_MspDeInit 1 */
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief SD MSP Initialization
|
||||
* This function configures the hardware resources used in this example
|
||||
* @param hsd: SD handle pointer
|
||||
* @retval None
|
||||
*/
|
||||
void HAL_SD_MspInit(SD_HandleTypeDef* hsd)
|
||||
{
|
||||
GPIO_InitTypeDef GPIO_InitStruct = {0};
|
||||
RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0};
|
||||
if(hsd->Instance==SDMMC1)
|
||||
{
|
||||
/* USER CODE BEGIN SDMMC1_MspInit 0 */
|
||||
|
||||
/* USER CODE END SDMMC1_MspInit 0 */
|
||||
|
||||
/** Initializes the peripherals clock
|
||||
*/
|
||||
PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_SDMMC1|RCC_PERIPHCLK_CLK48;
|
||||
PeriphClkInitStruct.Clk48ClockSelection = RCC_CLK48SOURCE_PLL;
|
||||
PeriphClkInitStruct.Sdmmc1ClockSelection = RCC_SDMMC1CLKSOURCE_CLK48;
|
||||
if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
|
||||
/* Peripheral clock enable */
|
||||
__HAL_RCC_SDMMC1_CLK_ENABLE();
|
||||
|
||||
__HAL_RCC_GPIOC_CLK_ENABLE();
|
||||
__HAL_RCC_GPIOD_CLK_ENABLE();
|
||||
/**SDMMC1 GPIO Configuration
|
||||
PC8 ------> SDMMC1_D0
|
||||
PC9 ------> SDMMC1_D1
|
||||
PC10 ------> SDMMC1_D2
|
||||
PC11 ------> SDMMC1_D3
|
||||
PC12 ------> SDMMC1_CK
|
||||
PD2 ------> SDMMC1_CMD
|
||||
*/
|
||||
GPIO_InitStruct.Pin = GPIO_PIN_8|GPIO_PIN_9|GPIO_PIN_10|GPIO_PIN_11
|
||||
|GPIO_PIN_12;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
|
||||
GPIO_InitStruct.Alternate = GPIO_AF12_SDMMC1;
|
||||
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
|
||||
|
||||
GPIO_InitStruct.Pin = GPIO_PIN_2;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
|
||||
GPIO_InitStruct.Alternate = GPIO_AF12_SDMMC1;
|
||||
HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
|
||||
|
||||
/* USER CODE BEGIN SDMMC1_MspInit 1 */
|
||||
|
||||
/* USER CODE END SDMMC1_MspInit 1 */
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief SD MSP De-Initialization
|
||||
* This function freeze the hardware resources used in this example
|
||||
* @param hsd: SD handle pointer
|
||||
* @retval None
|
||||
*/
|
||||
void HAL_SD_MspDeInit(SD_HandleTypeDef* hsd)
|
||||
{
|
||||
if(hsd->Instance==SDMMC1)
|
||||
{
|
||||
/* USER CODE BEGIN SDMMC1_MspDeInit 0 */
|
||||
|
||||
/* USER CODE END SDMMC1_MspDeInit 0 */
|
||||
/* Peripheral clock disable */
|
||||
__HAL_RCC_SDMMC1_CLK_DISABLE();
|
||||
|
||||
/**SDMMC1 GPIO Configuration
|
||||
PC8 ------> SDMMC1_D0
|
||||
PC9 ------> SDMMC1_D1
|
||||
PC10 ------> SDMMC1_D2
|
||||
PC11 ------> SDMMC1_D3
|
||||
PC12 ------> SDMMC1_CK
|
||||
PD2 ------> SDMMC1_CMD
|
||||
*/
|
||||
HAL_GPIO_DeInit(GPIOC, GPIO_PIN_8|GPIO_PIN_9|GPIO_PIN_10|GPIO_PIN_11
|
||||
|GPIO_PIN_12);
|
||||
|
||||
HAL_GPIO_DeInit(GPIOD, GPIO_PIN_2);
|
||||
|
||||
/* USER CODE BEGIN SDMMC1_MspDeInit 1 */
|
||||
|
||||
/* USER CODE END SDMMC1_MspDeInit 1 */
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief TIM_Base MSP Initialization
|
||||
* This function configures the hardware resources used in this example
|
||||
* @param htim_base: TIM_Base handle pointer
|
||||
* @retval None
|
||||
*/
|
||||
void HAL_TIM_Base_MspInit(TIM_HandleTypeDef* htim_base)
|
||||
{
|
||||
if(htim_base->Instance==TIM10)
|
||||
{
|
||||
/* USER CODE BEGIN TIM10_MspInit 0 */
|
||||
|
||||
/* USER CODE END TIM10_MspInit 0 */
|
||||
/* Peripheral clock enable */
|
||||
__HAL_RCC_TIM10_CLK_ENABLE();
|
||||
/* TIM10 interrupt Init */
|
||||
HAL_NVIC_SetPriority(TIM1_UP_TIM10_IRQn, 0, 0);
|
||||
HAL_NVIC_EnableIRQ(TIM1_UP_TIM10_IRQn);
|
||||
/* USER CODE BEGIN TIM10_MspInit 1 */
|
||||
|
||||
/* USER CODE END TIM10_MspInit 1 */
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief TIM_Base MSP De-Initialization
|
||||
* This function freeze the hardware resources used in this example
|
||||
* @param htim_base: TIM_Base handle pointer
|
||||
* @retval None
|
||||
*/
|
||||
void HAL_TIM_Base_MspDeInit(TIM_HandleTypeDef* htim_base)
|
||||
{
|
||||
if(htim_base->Instance==TIM10)
|
||||
{
|
||||
/* USER CODE BEGIN TIM10_MspDeInit 0 */
|
||||
|
||||
/* USER CODE END TIM10_MspDeInit 0 */
|
||||
/* Peripheral clock disable */
|
||||
__HAL_RCC_TIM10_CLK_DISABLE();
|
||||
|
||||
/* TIM10 interrupt DeInit */
|
||||
HAL_NVIC_DisableIRQ(TIM1_UP_TIM10_IRQn);
|
||||
/* USER CODE BEGIN TIM10_MspDeInit 1 */
|
||||
|
||||
/* USER CODE END TIM10_MspDeInit 1 */
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/* USER CODE BEGIN 1 */
|
||||
|
||||
/* USER CODE END 1 */
|
||||
496
Src/stm32f7xx_it.c
Normal file
496
Src/stm32f7xx_it.c
Normal file
@ -0,0 +1,496 @@
|
||||
/* USER CODE BEGIN Header */
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file stm32f7xx_it.c
|
||||
* @brief Interrupt Service Routines.
|
||||
******************************************************************************
|
||||
* @attention
|
||||
*
|
||||
* Copyright (c) 2023 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 "stm32f7xx_it.h"
|
||||
/* Private includes ----------------------------------------------------------*/
|
||||
/* USER CODE BEGIN Includes */
|
||||
/* USER CODE END Includes */
|
||||
|
||||
/* Private typedef -----------------------------------------------------------*/
|
||||
/* USER CODE BEGIN TD */
|
||||
|
||||
/* USER CODE END TD */
|
||||
|
||||
/* Private define ------------------------------------------------------------*/
|
||||
/* USER CODE BEGIN PD */
|
||||
|
||||
/* USER CODE END PD */
|
||||
|
||||
/* Private macro -------------------------------------------------------------*/
|
||||
/* USER CODE BEGIN PM */
|
||||
|
||||
/* USER CODE END PM */
|
||||
|
||||
/* Private variables ---------------------------------------------------------*/
|
||||
/* USER CODE BEGIN PV */
|
||||
extern uint32_t TO6, TO7, TO6_uart, TO10, TO10_counter;
|
||||
extern uint16_t UART_rec_incr, UART_header, COMMAND[CL_16];
|
||||
extern uint8_t uart_buf, flg_tmt, CPU_state, State_Data[2], UART_transmission_request, u_tx_flg, TIM10_coflag;
|
||||
extern task_t task;
|
||||
/* USER CODE END PV */
|
||||
|
||||
/* Private function prototypes -----------------------------------------------*/
|
||||
/* USER CODE BEGIN PFP */
|
||||
void UART_RxCpltCallback(void);
|
||||
void DMA2_Stream7_TransferComplete(void);
|
||||
/* USER CODE END PFP */
|
||||
|
||||
/* Private user code ---------------------------------------------------------*/
|
||||
/* USER CODE BEGIN 0 */
|
||||
|
||||
/* USER CODE END 0 */
|
||||
|
||||
/* External variables --------------------------------------------------------*/
|
||||
extern ADC_HandleTypeDef hadc1;
|
||||
extern ADC_HandleTypeDef hadc3;
|
||||
extern TIM_HandleTypeDef htim10;
|
||||
/* USER CODE BEGIN EV */
|
||||
|
||||
/* USER CODE END EV */
|
||||
|
||||
/******************************************************************************/
|
||||
/* Cortex-M7 Processor Interruption and Exception Handlers */
|
||||
/******************************************************************************/
|
||||
/**
|
||||
* @brief This function handles Non maskable interrupt.
|
||||
*/
|
||||
void NMI_Handler(void)
|
||||
{
|
||||
/* USER CODE BEGIN NonMaskableInt_IRQn 0 */
|
||||
|
||||
/* USER CODE END NonMaskableInt_IRQn 0 */
|
||||
/* USER CODE BEGIN NonMaskableInt_IRQn 1 */
|
||||
while (1)
|
||||
{
|
||||
}
|
||||
/* USER CODE END NonMaskableInt_IRQn 1 */
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief This function handles Hard fault interrupt.
|
||||
*/
|
||||
void HardFault_Handler(void)
|
||||
{
|
||||
/* USER CODE BEGIN HardFault_IRQn 0 */
|
||||
|
||||
/* USER CODE END HardFault_IRQn 0 */
|
||||
while (1)
|
||||
{
|
||||
/* USER CODE BEGIN W1_HardFault_IRQn 0 */
|
||||
/* USER CODE END W1_HardFault_IRQn 0 */
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief This function handles Memory management fault.
|
||||
*/
|
||||
void MemManage_Handler(void)
|
||||
{
|
||||
/* USER CODE BEGIN MemoryManagement_IRQn 0 */
|
||||
|
||||
/* USER CODE END MemoryManagement_IRQn 0 */
|
||||
while (1)
|
||||
{
|
||||
/* USER CODE BEGIN W1_MemoryManagement_IRQn 0 */
|
||||
/* USER CODE END W1_MemoryManagement_IRQn 0 */
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief This function handles Pre-fetch fault, memory access fault.
|
||||
*/
|
||||
void BusFault_Handler(void)
|
||||
{
|
||||
/* USER CODE BEGIN BusFault_IRQn 0 */
|
||||
|
||||
/* USER CODE END BusFault_IRQn 0 */
|
||||
while (1)
|
||||
{
|
||||
/* USER CODE BEGIN W1_BusFault_IRQn 0 */
|
||||
/* USER CODE END W1_BusFault_IRQn 0 */
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief This function handles Undefined instruction or illegal state.
|
||||
*/
|
||||
void UsageFault_Handler(void)
|
||||
{
|
||||
/* USER CODE BEGIN UsageFault_IRQn 0 */
|
||||
|
||||
/* USER CODE END UsageFault_IRQn 0 */
|
||||
while (1)
|
||||
{
|
||||
/* USER CODE BEGIN W1_UsageFault_IRQn 0 */
|
||||
/* USER CODE END W1_UsageFault_IRQn 0 */
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief This function handles System service call via SWI instruction.
|
||||
*/
|
||||
void SVC_Handler(void)
|
||||
{
|
||||
/* USER CODE BEGIN SVCall_IRQn 0 */
|
||||
|
||||
/* USER CODE END SVCall_IRQn 0 */
|
||||
/* USER CODE BEGIN SVCall_IRQn 1 */
|
||||
|
||||
/* USER CODE END SVCall_IRQn 1 */
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief This function handles Debug monitor.
|
||||
*/
|
||||
void DebugMon_Handler(void)
|
||||
{
|
||||
/* USER CODE BEGIN DebugMonitor_IRQn 0 */
|
||||
|
||||
/* USER CODE END DebugMonitor_IRQn 0 */
|
||||
/* USER CODE BEGIN DebugMonitor_IRQn 1 */
|
||||
|
||||
/* USER CODE END DebugMonitor_IRQn 1 */
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief This function handles Pendable request for system service.
|
||||
*/
|
||||
void PendSV_Handler(void)
|
||||
{
|
||||
/* USER CODE BEGIN PendSV_IRQn 0 */
|
||||
|
||||
/* USER CODE END PendSV_IRQn 0 */
|
||||
/* USER CODE BEGIN PendSV_IRQn 1 */
|
||||
|
||||
/* USER CODE END PendSV_IRQn 1 */
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief This function handles System tick timer.
|
||||
*/
|
||||
void SysTick_Handler(void)
|
||||
{
|
||||
/* USER CODE BEGIN SysTick_IRQn 0 */
|
||||
|
||||
/* USER CODE END SysTick_IRQn 0 */
|
||||
HAL_IncTick();
|
||||
/* USER CODE BEGIN SysTick_IRQn 1 */
|
||||
|
||||
/* USER CODE END SysTick_IRQn 1 */
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/* STM32F7xx Peripheral Interrupt Handlers */
|
||||
/* Add here the Interrupt Handlers for the used peripherals. */
|
||||
/* For the available peripheral interrupt handler names, */
|
||||
/* please refer to the startup file (startup_stm32f7xx.s). */
|
||||
/******************************************************************************/
|
||||
|
||||
/**
|
||||
* @brief This function handles ADC1, ADC2 and ADC3 global interrupts.
|
||||
*/
|
||||
void ADC_IRQHandler(void)
|
||||
{
|
||||
/* USER CODE BEGIN ADC_IRQn 0 */
|
||||
|
||||
/* USER CODE END ADC_IRQn 0 */
|
||||
HAL_ADC_IRQHandler(&hadc1);
|
||||
HAL_ADC_IRQHandler(&hadc3);
|
||||
/* USER CODE BEGIN ADC_IRQn 1 */
|
||||
|
||||
/* USER CODE END ADC_IRQn 1 */
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief This function handles TIM1 update interrupt and TIM10 global interrupt.
|
||||
*/
|
||||
void TIM1_UP_TIM10_IRQHandler(void)
|
||||
{
|
||||
/* USER CODE BEGIN TIM1_UP_TIM10_IRQn 0 */
|
||||
TO10++;
|
||||
if (TO10 == TO10_counter)
|
||||
TIM10_coflag = 1;
|
||||
/* USER CODE END TIM1_UP_TIM10_IRQn 0 */
|
||||
HAL_TIM_IRQHandler(&htim10);
|
||||
/* USER CODE BEGIN TIM1_UP_TIM10_IRQn 1 */
|
||||
|
||||
/* USER CODE END TIM1_UP_TIM10_IRQn 1 */
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief This function handles TIM2 global interrupt.
|
||||
*/
|
||||
void TIM2_IRQHandler(void)
|
||||
{
|
||||
/* USER CODE BEGIN TIM2_IRQn 0 */
|
||||
|
||||
/* USER CODE END TIM2_IRQn 0 */
|
||||
/* USER CODE BEGIN TIM2_IRQn 1 */
|
||||
|
||||
/* USER CODE END TIM2_IRQn 1 */
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief This function handles USART1 global interrupt.
|
||||
*/
|
||||
void USART1_IRQHandler(void)
|
||||
{
|
||||
/* USER CODE BEGIN USART1_IRQn 0 */
|
||||
volatile uint8_t temp;
|
||||
if(LL_USART_IsActiveFlag_RXNE(USART1) && LL_USART_IsEnabledIT_RXNE(USART1))
|
||||
{
|
||||
UART_RxCpltCallback();
|
||||
}
|
||||
else
|
||||
{
|
||||
if(LL_USART_IsActiveFlag_ORE(USART1))
|
||||
{
|
||||
//temp = USART1->RDR;
|
||||
temp+= LL_USART_ReceiveData8(USART1);
|
||||
}
|
||||
else if(LL_USART_IsActiveFlag_FE(USART1))
|
||||
{
|
||||
//(void) USART1->RDR;
|
||||
temp+= LL_USART_ReceiveData8(USART1);
|
||||
}
|
||||
else if(LL_USART_IsActiveFlag_NE(USART1))
|
||||
{
|
||||
//(void) USART1->RDR;
|
||||
temp+= LL_USART_ReceiveData8(USART1);
|
||||
}
|
||||
else if(LL_USART_IsActiveFlag_PE(USART1))
|
||||
{
|
||||
//(void) USART1->RDR;
|
||||
temp+= LL_USART_ReceiveData8(USART1);
|
||||
}
|
||||
else
|
||||
{
|
||||
if(LL_USART_IsActiveFlag_TC(USART6) && LL_USART_IsEnabledIT_TC(USART6))
|
||||
{
|
||||
LL_USART_ClearFlag_TC(USART1);
|
||||
//test_counter += 1;
|
||||
//if(UART_transmission_busy == 1){
|
||||
LL_USART_DisableIT_TC(USART1);
|
||||
//UART_transmission_busy = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* USER CODE END USART1_IRQn 0 */
|
||||
/* USER CODE BEGIN USART1_IRQn 1 */
|
||||
|
||||
/* USER CODE END USART1_IRQn 1 */
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief This function handles TIM5 global interrupt.
|
||||
*/
|
||||
void TIM5_IRQHandler(void)
|
||||
{
|
||||
/* USER CODE BEGIN TIM5_IRQn 0 */
|
||||
|
||||
/* USER CODE END TIM5_IRQn 0 */
|
||||
/* USER CODE BEGIN TIM5_IRQn 1 */
|
||||
|
||||
/* USER CODE END TIM5_IRQn 1 */
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief This function handles TIM6 global interrupt, DAC1 and DAC2 underrun error interrupts.
|
||||
*/
|
||||
void TIM6_DAC_IRQHandler(void)
|
||||
{
|
||||
/* USER CODE BEGIN TIM6_DAC_IRQn 0 */
|
||||
|
||||
/* USER CODE END TIM6_DAC_IRQn 0 */
|
||||
|
||||
/* USER CODE BEGIN TIM6_DAC_IRQn 1 */
|
||||
if(LL_TIM_IsActiveFlag_UPDATE(TIM6))
|
||||
{
|
||||
LL_TIM_ClearFlag_UPDATE(TIM6);
|
||||
TO6++;//increment tick
|
||||
//10 ms or 100 Hz
|
||||
HAL_GPIO_TogglePin(TEST_01_GPIO_Port, TEST_01_Pin);
|
||||
//HAL_GPIO_TogglePin(GPIOD, GPIO_PIN_12);
|
||||
}
|
||||
/* USER CODE END TIM6_DAC_IRQn 1 */
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief This function handles TIM7 global interrupt.
|
||||
*/
|
||||
void TIM7_IRQHandler(void)
|
||||
{
|
||||
/* USER CODE BEGIN TIM7_IRQn 0 */
|
||||
|
||||
/* USER CODE END TIM7_IRQn 0 */
|
||||
/* USER CODE BEGIN TIM7_IRQn 1 */
|
||||
if(LL_TIM_IsActiveFlag_UPDATE(TIM7))
|
||||
{
|
||||
LL_TIM_ClearFlag_UPDATE(TIM7);
|
||||
TO7++;
|
||||
//1 ms or 1000 Hz
|
||||
//HAL_GPIO_TogglePin(TEST_01_GPIO_Port, TEST_01_Pin);
|
||||
}
|
||||
/* USER CODE END TIM7_IRQn 1 */
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief This function handles DMA2 stream7 global interrupt.
|
||||
*/
|
||||
void DMA2_Stream7_IRQHandler(void)
|
||||
{
|
||||
/* USER CODE BEGIN DMA2_Stream7_IRQn 0 */
|
||||
if(LL_DMA_IsActiveFlag_TC7(DMA2) == 1)
|
||||
{
|
||||
DMA2_Stream7_TransferComplete();
|
||||
u_tx_flg = 0;//indicate that transfer compete
|
||||
}
|
||||
else if(LL_DMA_IsActiveFlag_TE7(DMA2) == 1)
|
||||
{
|
||||
LL_DMA_ClearFlag_TE7(DMA2);
|
||||
}
|
||||
/* USER CODE END DMA2_Stream7_IRQn 0 */
|
||||
|
||||
/* USER CODE BEGIN DMA2_Stream7_IRQn 1 */
|
||||
|
||||
/* USER CODE END DMA2_Stream7_IRQn 1 */
|
||||
}
|
||||
|
||||
/* USER CODE BEGIN 1 */
|
||||
void UART_RxCpltCallback(void)
|
||||
{
|
||||
uart_buf = LL_USART_ReceiveData8(USART1);
|
||||
switch (UART_rec_incr)
|
||||
{
|
||||
case 0:
|
||||
TO6_uart = TO6;//Save the time of start rec. command
|
||||
flg_tmt = 1;//Set the timeout flag
|
||||
UART_header = uart_buf;
|
||||
UART_rec_incr++;
|
||||
break;
|
||||
case 1:
|
||||
UART_header += ((uint16_t)uart_buf<<8);
|
||||
switch (UART_header)
|
||||
{
|
||||
case 0x1111: //received long packet
|
||||
UART_rec_incr = 2;//timeout flag is still setting!
|
||||
break;
|
||||
case 0x2222: //Back to default
|
||||
UART_rec_incr = 0;
|
||||
flg_tmt = 0;//Reset the timeout flag
|
||||
CPU_state = DEFAULT_ENABLE;
|
||||
break;
|
||||
case 0x3333: //Transmith saved DATA
|
||||
UART_rec_incr = 0;
|
||||
flg_tmt = 0;//Reset the timeout flag
|
||||
CPU_state = TRANS_S_ENABLE;
|
||||
break;
|
||||
case 0x4444: //Received packet
|
||||
UART_rec_incr = 0;
|
||||
flg_tmt = 0;//Reset the timeout flag
|
||||
CPU_state = TRANS_ENABLE;
|
||||
break;
|
||||
case 0x5555: //Erase saved DATA
|
||||
UART_rec_incr = 0;
|
||||
flg_tmt = 0;//Reset the timeout flag
|
||||
CPU_state = REMOVE_FILE;
|
||||
break;
|
||||
case 0x6666: //Request state
|
||||
UART_rec_incr = 0;
|
||||
flg_tmt = 0;//Reset the timeout flag
|
||||
CPU_state = STATE;
|
||||
break;
|
||||
case 0x7777:
|
||||
UART_rec_incr = 2;//timeout flag is still setting!
|
||||
break;
|
||||
default: //error decoding header
|
||||
UART_rec_incr = 0;
|
||||
flg_tmt = 0;//Reset the timeout flag
|
||||
//UART_transmission_request = MESS_01;
|
||||
//CPU_state = HALT;
|
||||
State_Data[0] |= UART_ERR;
|
||||
CPU_state = DEFAULT_ENABLE;//Parking system and send error state!
|
||||
break;
|
||||
}
|
||||
break;
|
||||
|
||||
case (CL_8 - 1):
|
||||
if (UART_header == 0x1111)
|
||||
{
|
||||
if ((UART_rec_incr & 0x0001) > 0)
|
||||
COMMAND[(UART_rec_incr >> 1) - 1] += ((uint16_t)(uart_buf)) << 8;
|
||||
else
|
||||
COMMAND[(UART_rec_incr >> 1) - 1] = (uint16_t)(uart_buf);
|
||||
CPU_state = DECODE_ENABLE;
|
||||
UART_rec_incr = 0;
|
||||
flg_tmt = 0;//Reset the timeout flag
|
||||
}
|
||||
else
|
||||
{
|
||||
if ((UART_rec_incr&0x0001)>0)
|
||||
COMMAND[(UART_rec_incr>>1)-1] += ((uint16_t)(uart_buf))<<8;
|
||||
else
|
||||
COMMAND[(UART_rec_incr>>1)-1] = (uint16_t)(uart_buf);
|
||||
UART_rec_incr++;
|
||||
UART_transmission_request = NO_MESS;
|
||||
}
|
||||
break;
|
||||
case (TSK_8 - 1):
|
||||
if (UART_header == 0x7777)
|
||||
{
|
||||
if ((UART_rec_incr&0x0001)>0)
|
||||
COMMAND[(UART_rec_incr>>1)-1] += ((uint16_t)(uart_buf))<<8;
|
||||
else
|
||||
COMMAND[(UART_rec_incr>>1)-1] = (uint16_t)(uart_buf);
|
||||
CPU_state = DECODE_TASK;
|
||||
UART_rec_incr = 0;
|
||||
flg_tmt = 0;//Reset the timeout flag
|
||||
}
|
||||
else
|
||||
{
|
||||
if ((UART_rec_incr&0x0001)>0)
|
||||
COMMAND[(UART_rec_incr>>1)-1] += ((uint16_t)(uart_buf))<<8;
|
||||
else
|
||||
COMMAND[(UART_rec_incr>>1)-1] = (uint16_t)(uart_buf);
|
||||
UART_rec_incr++;
|
||||
UART_transmission_request = NO_MESS;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
if ((UART_rec_incr&0x0001)>0)
|
||||
COMMAND[(UART_rec_incr>>1)-1] += ((uint16_t)(uart_buf))<<8;
|
||||
else
|
||||
COMMAND[(UART_rec_incr>>1)-1] = (uint16_t)(uart_buf);
|
||||
UART_rec_incr++;
|
||||
UART_transmission_request = NO_MESS;
|
||||
break;
|
||||
}
|
||||
// HAL_UART_Receive_IT(&huart1, &uart_buf, 1);
|
||||
}
|
||||
|
||||
//-----------------------------------------------
|
||||
void DMA2_Stream7_TransferComplete(void)
|
||||
{
|
||||
LL_DMA_ClearFlag_TC7(DMA2);
|
||||
}
|
||||
//-----------------------------------------------
|
||||
/* USER CODE END 1 */
|
||||
176
Src/syscalls.c
Normal file
176
Src/syscalls.c
Normal file
@ -0,0 +1,176 @@
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file syscalls.c
|
||||
* @author Auto-generated by STM32CubeMX
|
||||
* @brief Minimal System calls file
|
||||
*
|
||||
* For more information about which c-functions
|
||||
* need which of these lowlevel functions
|
||||
* please consult the Newlib libc-manual
|
||||
******************************************************************************
|
||||
* @attention
|
||||
*
|
||||
* Copyright (c) 2020-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.
|
||||
*
|
||||
******************************************************************************
|
||||
*/
|
||||
|
||||
/* Includes */
|
||||
#include <sys/stat.h>
|
||||
#include <stdlib.h>
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include <signal.h>
|
||||
#include <time.h>
|
||||
#include <sys/time.h>
|
||||
#include <sys/times.h>
|
||||
|
||||
|
||||
/* Variables */
|
||||
extern int __io_putchar(int ch) __attribute__((weak));
|
||||
extern int __io_getchar(void) __attribute__((weak));
|
||||
|
||||
|
||||
char *__env[1] = { 0 };
|
||||
char **environ = __env;
|
||||
|
||||
|
||||
/* Functions */
|
||||
void initialise_monitor_handles()
|
||||
{
|
||||
}
|
||||
|
||||
int _getpid(void)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
int _kill(int pid, int sig)
|
||||
{
|
||||
(void)pid;
|
||||
(void)sig;
|
||||
errno = EINVAL;
|
||||
return -1;
|
||||
}
|
||||
|
||||
void _exit (int status)
|
||||
{
|
||||
_kill(status, -1);
|
||||
while (1) {} /* Make sure we hang here */
|
||||
}
|
||||
|
||||
__attribute__((weak)) int _read(int file, char *ptr, int len)
|
||||
{
|
||||
(void)file;
|
||||
int DataIdx;
|
||||
|
||||
for (DataIdx = 0; DataIdx < len; DataIdx++)
|
||||
{
|
||||
*ptr++ = __io_getchar();
|
||||
}
|
||||
|
||||
return len;
|
||||
}
|
||||
|
||||
__attribute__((weak)) int _write(int file, char *ptr, int len)
|
||||
{
|
||||
(void)file;
|
||||
int DataIdx;
|
||||
|
||||
for (DataIdx = 0; DataIdx < len; DataIdx++)
|
||||
{
|
||||
__io_putchar(*ptr++);
|
||||
}
|
||||
return len;
|
||||
}
|
||||
|
||||
int _close(int file)
|
||||
{
|
||||
(void)file;
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
int _fstat(int file, struct stat *st)
|
||||
{
|
||||
(void)file;
|
||||
st->st_mode = S_IFCHR;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int _isatty(int file)
|
||||
{
|
||||
(void)file;
|
||||
return 1;
|
||||
}
|
||||
|
||||
int _lseek(int file, int ptr, int dir)
|
||||
{
|
||||
(void)file;
|
||||
(void)ptr;
|
||||
(void)dir;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int _open(char *path, int flags, ...)
|
||||
{
|
||||
(void)path;
|
||||
(void)flags;
|
||||
/* Pretend like we always fail */
|
||||
return -1;
|
||||
}
|
||||
|
||||
int _wait(int *status)
|
||||
{
|
||||
(void)status;
|
||||
errno = ECHILD;
|
||||
return -1;
|
||||
}
|
||||
|
||||
int _unlink(char *name)
|
||||
{
|
||||
(void)name;
|
||||
errno = ENOENT;
|
||||
return -1;
|
||||
}
|
||||
|
||||
int _times(struct tms *buf)
|
||||
{
|
||||
(void)buf;
|
||||
return -1;
|
||||
}
|
||||
|
||||
int _stat(char *file, struct stat *st)
|
||||
{
|
||||
(void)file;
|
||||
st->st_mode = S_IFCHR;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int _link(char *old, char *new)
|
||||
{
|
||||
(void)old;
|
||||
(void)new;
|
||||
errno = EMLINK;
|
||||
return -1;
|
||||
}
|
||||
|
||||
int _fork(void)
|
||||
{
|
||||
errno = EAGAIN;
|
||||
return -1;
|
||||
}
|
||||
|
||||
int _execve(char *name, char **argv, char **env)
|
||||
{
|
||||
(void)name;
|
||||
(void)argv;
|
||||
(void)env;
|
||||
errno = ENOMEM;
|
||||
return -1;
|
||||
}
|
||||
79
Src/sysmem.c
Normal file
79
Src/sysmem.c
Normal file
@ -0,0 +1,79 @@
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file sysmem.c
|
||||
* @author Generated by STM32CubeMX
|
||||
* @brief System Memory calls file
|
||||
*
|
||||
* For more information about which C functions
|
||||
* need which of these lowlevel functions
|
||||
* please consult the newlib libc manual
|
||||
******************************************************************************
|
||||
* @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.
|
||||
*
|
||||
******************************************************************************
|
||||
*/
|
||||
|
||||
/* Includes */
|
||||
#include <errno.h>
|
||||
#include <stdint.h>
|
||||
|
||||
/**
|
||||
* Pointer to the current high watermark of the heap usage
|
||||
*/
|
||||
static uint8_t *__sbrk_heap_end = NULL;
|
||||
|
||||
/**
|
||||
* @brief _sbrk() allocates memory to the newlib heap and is used by malloc
|
||||
* and others from the C library
|
||||
*
|
||||
* @verbatim
|
||||
* ############################################################################
|
||||
* # .data # .bss # newlib heap # MSP stack #
|
||||
* # # # # Reserved by _Min_Stack_Size #
|
||||
* ############################################################################
|
||||
* ^-- RAM start ^-- _end _estack, RAM end --^
|
||||
* @endverbatim
|
||||
*
|
||||
* This implementation starts allocating at the '_end' linker symbol
|
||||
* The '_Min_Stack_Size' linker symbol reserves a memory for the MSP stack
|
||||
* The implementation considers '_estack' linker symbol to be RAM end
|
||||
* NOTE: If the MSP stack, at any point during execution, grows larger than the
|
||||
* reserved size, please increase the '_Min_Stack_Size'.
|
||||
*
|
||||
* @param incr Memory size
|
||||
* @return Pointer to allocated memory
|
||||
*/
|
||||
void *_sbrk(ptrdiff_t incr)
|
||||
{
|
||||
extern uint8_t _end; /* Symbol defined in the linker script */
|
||||
extern uint8_t _estack; /* Symbol defined in the linker script */
|
||||
extern uint32_t _Min_Stack_Size; /* Symbol defined in the linker script */
|
||||
const uint32_t stack_limit = (uint32_t)&_estack - (uint32_t)&_Min_Stack_Size;
|
||||
const uint8_t *max_heap = (uint8_t *)stack_limit;
|
||||
uint8_t *prev_heap_end;
|
||||
|
||||
/* Initialize heap end at first call */
|
||||
if (NULL == __sbrk_heap_end)
|
||||
{
|
||||
__sbrk_heap_end = &_end;
|
||||
}
|
||||
|
||||
/* Protect heap from growing into the reserved MSP stack */
|
||||
if (__sbrk_heap_end + incr > max_heap)
|
||||
{
|
||||
errno = ENOMEM;
|
||||
return (void *)-1;
|
||||
}
|
||||
|
||||
prev_heap_end = __sbrk_heap_end;
|
||||
__sbrk_heap_end += incr;
|
||||
|
||||
return (void *)prev_heap_end;
|
||||
}
|
||||
259
Src/system_stm32f7xx.c
Normal file
259
Src/system_stm32f7xx.c
Normal file
@ -0,0 +1,259 @@
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file system_stm32f7xx.c
|
||||
* @author MCD Application Team
|
||||
* @brief CMSIS Cortex-M7 Device Peripheral Access Layer System Source File.
|
||||
*
|
||||
* This file provides two functions and one global variable to be called from
|
||||
* user application:
|
||||
* - SystemInit(): This function is called at startup just after reset and
|
||||
* before branch to main program. This call is made inside
|
||||
* the "startup_stm32f7xx.s" file.
|
||||
*
|
||||
* - SystemCoreClock variable: Contains the core clock (HCLK), it can be used
|
||||
* by the user application to setup the SysTick
|
||||
* timer or configure other parameters.
|
||||
*
|
||||
* - SystemCoreClockUpdate(): Updates the variable SystemCoreClock and must
|
||||
* be called whenever the core clock is changed
|
||||
* during program execution.
|
||||
*
|
||||
*
|
||||
******************************************************************************
|
||||
* @attention
|
||||
*
|
||||
* Copyright (c) 2016 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.
|
||||
*
|
||||
******************************************************************************
|
||||
*/
|
||||
|
||||
/** @addtogroup CMSIS
|
||||
* @{
|
||||
*/
|
||||
|
||||
/** @addtogroup stm32f7xx_system
|
||||
* @{
|
||||
*/
|
||||
|
||||
/** @addtogroup STM32F7xx_System_Private_Includes
|
||||
* @{
|
||||
*/
|
||||
|
||||
#include "stm32f7xx.h"
|
||||
|
||||
#if !defined (HSE_VALUE)
|
||||
#define HSE_VALUE ((uint32_t)25000000) /*!< Default value of the External oscillator in Hz */
|
||||
#endif /* HSE_VALUE */
|
||||
|
||||
#if !defined (HSI_VALUE)
|
||||
#define HSI_VALUE ((uint32_t)16000000) /*!< Value of the Internal oscillator in Hz*/
|
||||
#endif /* HSI_VALUE */
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/** @addtogroup STM32F7xx_System_Private_TypesDefinitions
|
||||
* @{
|
||||
*/
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/** @addtogroup STM32F7xx_System_Private_Defines
|
||||
* @{
|
||||
*/
|
||||
|
||||
/************************* Miscellaneous Configuration ************************/
|
||||
|
||||
/* Note: Following vector table addresses must be defined in line with linker
|
||||
configuration. */
|
||||
/*!< Uncomment the following line if you need to relocate the vector table
|
||||
anywhere in Flash or Sram, else the vector table is kept at the automatic
|
||||
remap of boot address selected */
|
||||
/* #define USER_VECT_TAB_ADDRESS */
|
||||
|
||||
#if defined(USER_VECT_TAB_ADDRESS)
|
||||
/*!< Uncomment the following line if you need to relocate your vector Table
|
||||
in Sram else user remap will be done in Flash. */
|
||||
/* #define VECT_TAB_SRAM */
|
||||
#if defined(VECT_TAB_SRAM)
|
||||
#define VECT_TAB_BASE_ADDRESS RAMDTCM_BASE /*!< Vector Table base address field.
|
||||
This value must be a multiple of 0x200. */
|
||||
#define VECT_TAB_OFFSET 0x00000000U /*!< Vector Table base offset field.
|
||||
This value must be a multiple of 0x200. */
|
||||
#else
|
||||
#define VECT_TAB_BASE_ADDRESS FLASH_BASE /*!< Vector Table base address field.
|
||||
This value must be a multiple of 0x200. */
|
||||
#define VECT_TAB_OFFSET 0x00000000U /*!< Vector Table base offset field.
|
||||
This value must be a multiple of 0x200. */
|
||||
#endif /* VECT_TAB_SRAM */
|
||||
#endif /* USER_VECT_TAB_ADDRESS */
|
||||
/******************************************************************************/
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/** @addtogroup STM32F7xx_System_Private_Macros
|
||||
* @{
|
||||
*/
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/** @addtogroup STM32F7xx_System_Private_Variables
|
||||
* @{
|
||||
*/
|
||||
|
||||
/* This variable is updated in three ways:
|
||||
1) by calling CMSIS function SystemCoreClockUpdate()
|
||||
2) by calling HAL API function HAL_RCC_GetHCLKFreq()
|
||||
3) each time HAL_RCC_ClockConfig() is called to configure the system clock frequency
|
||||
Note: If you use this function to configure the system clock; then there
|
||||
is no need to call the 2 first functions listed above, since SystemCoreClock
|
||||
variable is updated automatically.
|
||||
*/
|
||||
uint32_t SystemCoreClock = 16000000;
|
||||
const uint8_t AHBPrescTable[16] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 7, 8, 9};
|
||||
const uint8_t APBPrescTable[8] = {0, 0, 0, 0, 1, 2, 3, 4};
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/** @addtogroup STM32F7xx_System_Private_FunctionPrototypes
|
||||
* @{
|
||||
*/
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/** @addtogroup STM32F7xx_System_Private_Functions
|
||||
* @{
|
||||
*/
|
||||
|
||||
/**
|
||||
* @brief Setup the microcontroller system
|
||||
* Initialize the Embedded Flash Interface, the PLL and update the
|
||||
* SystemFrequency variable.
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
void SystemInit(void)
|
||||
{
|
||||
/* FPU settings ------------------------------------------------------------*/
|
||||
#if (__FPU_PRESENT == 1) && (__FPU_USED == 1)
|
||||
SCB->CPACR |= ((3UL << 10*2)|(3UL << 11*2)); /* set CP10 and CP11 Full Access */
|
||||
#endif
|
||||
|
||||
/* Configure the Vector Table location -------------------------------------*/
|
||||
#if defined(USER_VECT_TAB_ADDRESS)
|
||||
SCB->VTOR = VECT_TAB_BASE_ADDRESS | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal SRAM */
|
||||
#endif /* USER_VECT_TAB_ADDRESS */
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Update SystemCoreClock variable according to Clock Register Values.
|
||||
* The SystemCoreClock variable contains the core clock (HCLK), it can
|
||||
* be used by the user application to setup the SysTick timer or configure
|
||||
* other parameters.
|
||||
*
|
||||
* @note Each time the core clock (HCLK) changes, this function must be called
|
||||
* to update SystemCoreClock variable value. Otherwise, any configuration
|
||||
* based on this variable will be incorrect.
|
||||
*
|
||||
* @note - The system frequency computed by this function is not the real
|
||||
* frequency in the chip. It is calculated based on the predefined
|
||||
* constant and the selected clock source:
|
||||
*
|
||||
* - If SYSCLK source is HSI, SystemCoreClock will contain the HSI_VALUE(*)
|
||||
*
|
||||
* - If SYSCLK source is HSE, SystemCoreClock will contain the HSE_VALUE(**)
|
||||
*
|
||||
* - If SYSCLK source is PLL, SystemCoreClock will contain the HSE_VALUE(**)
|
||||
* or HSI_VALUE(*) multiplied/divided by the PLL factors.
|
||||
*
|
||||
* (*) HSI_VALUE is a constant defined in stm32f7xx_hal_conf.h file (default value
|
||||
* 16 MHz) but the real value may vary depending on the variations
|
||||
* in voltage and temperature.
|
||||
*
|
||||
* (**) HSE_VALUE is a constant defined in stm32f7xx_hal_conf.h file (default value
|
||||
* 25 MHz), user has to ensure that HSE_VALUE is same as the real
|
||||
* frequency of the crystal used. Otherwise, this function may
|
||||
* have wrong result.
|
||||
*
|
||||
* - The result of this function could be not correct when using fractional
|
||||
* value for HSE crystal.
|
||||
*
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
void SystemCoreClockUpdate(void)
|
||||
{
|
||||
uint32_t tmp = 0, pllvco = 0, pllp = 2, pllsource = 0, pllm = 2;
|
||||
|
||||
/* Get SYSCLK source -------------------------------------------------------*/
|
||||
tmp = RCC->CFGR & RCC_CFGR_SWS;
|
||||
|
||||
switch (tmp)
|
||||
{
|
||||
case 0x00: /* HSI used as system clock source */
|
||||
SystemCoreClock = HSI_VALUE;
|
||||
break;
|
||||
case 0x04: /* HSE used as system clock source */
|
||||
SystemCoreClock = HSE_VALUE;
|
||||
break;
|
||||
case 0x08: /* PLL used as system clock source */
|
||||
|
||||
/* PLL_VCO = (HSE_VALUE or HSI_VALUE / PLL_M) * PLL_N
|
||||
SYSCLK = PLL_VCO / PLL_P
|
||||
*/
|
||||
pllsource = (RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC) >> 22;
|
||||
pllm = RCC->PLLCFGR & RCC_PLLCFGR_PLLM;
|
||||
|
||||
if (pllsource != 0)
|
||||
{
|
||||
/* HSE used as PLL clock source */
|
||||
pllvco = (HSE_VALUE / pllm) * ((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> 6);
|
||||
}
|
||||
else
|
||||
{
|
||||
/* HSI used as PLL clock source */
|
||||
pllvco = (HSI_VALUE / pllm) * ((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> 6);
|
||||
}
|
||||
|
||||
pllp = (((RCC->PLLCFGR & RCC_PLLCFGR_PLLP) >>16) + 1 ) *2;
|
||||
SystemCoreClock = pllvco/pllp;
|
||||
break;
|
||||
default:
|
||||
SystemCoreClock = HSI_VALUE;
|
||||
break;
|
||||
}
|
||||
/* Compute HCLK frequency --------------------------------------------------*/
|
||||
/* Get HCLK prescaler */
|
||||
tmp = AHBPrescTable[((RCC->CFGR & RCC_CFGR_HPRE) >> 4)];
|
||||
/* HCLK frequency */
|
||||
SystemCoreClock >>= tmp;
|
||||
}
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
|
||||
Reference in New Issue
Block a user