177 lines
4.3 KiB
C
177 lines
4.3 KiB
C
//#############################################################################
|
||
//
|
||
// FILE: sd_fat32.c
|
||
//
|
||
// TITLE: SD FATFS Library Example
|
||
//
|
||
//! Данная програма читает данные из текстового файла INPUT.txt и записывает данные в тектовый файл OUTPUT.txt.
|
||
//! *ВНИМАНИЕ другие форматы файловой системы не поддерживаются, если файл INPUT.txt не прописан програма зависнет.
|
||
//! для работы используется библиотекит TI:
|
||
//! driverlib.lib - для взаимодейсвитя с SD карточками по SPI
|
||
//! fatfs.lib - для работы с файловой системой FAT32.
|
||
//
|
||
//#############################################################################
|
||
//
|
||
// Included Files
|
||
//
|
||
#include "driverlib.h"
|
||
#include "device.h"
|
||
#include "board.h"
|
||
|
||
#include <sdspi/sdspi.h>
|
||
#include <sdspi/SDFatFS.h>
|
||
|
||
uint16_t SDFatFS_config_count = 1;
|
||
SDFatFS_Object sdfatfsObject;
|
||
|
||
SDSPI_Object sdspiObject = {
|
||
.spiHandle = mySDCardSPI_BASE,
|
||
.spiCsGpioIndex = mySDCardCS
|
||
};
|
||
|
||
SDFatFS_Object* SDFatFS_config [] = {&sdfatfsObject};
|
||
|
||
SDSPI_Handle sdspiHandle = &sdspiObject;
|
||
|
||
#define READ_BUFFER_SIZE 128 // Размер буфера для чтения
|
||
/* String conversion macro */
|
||
#define STR_(n) #n
|
||
#define STR(n) STR_(n)
|
||
|
||
/* Drive number used for FatFs */
|
||
#define DRIVE_NUM 0
|
||
|
||
const char inputfile[] = STR(DRIVE_NUM)":input.txt";
|
||
const char outputfile[] = STR(DRIVE_NUM)":output.txt";
|
||
const char textarray[] = "hi nima!";
|
||
DWORD freeClusterCount;
|
||
DWORD totalSectorCount;
|
||
DWORD freeSectorCount;
|
||
|
||
unsigned int bytesWritten = 0;
|
||
|
||
FIL src;
|
||
FIL dst;
|
||
|
||
unsigned int filesize;
|
||
FRESULT fresult;
|
||
|
||
//
|
||
// Main
|
||
//
|
||
void main(void)
|
||
{
|
||
//
|
||
// Initialize device clock and peripherals
|
||
//
|
||
Device_init();
|
||
|
||
//
|
||
// Disable pin locks and enable internal pullups.
|
||
//
|
||
Device_initGPIO();
|
||
|
||
//
|
||
// Initialize PIE and clear PIE registers. Disables CPU interrupts.
|
||
//
|
||
Interrupt_initModule();
|
||
|
||
//
|
||
// Initialize the PIE vector table with pointers to the shell Interrupt
|
||
// Service Routines (ISR).
|
||
//
|
||
Interrupt_initVectorTable();
|
||
|
||
//
|
||
// Board initialization
|
||
//
|
||
Board_init();
|
||
|
||
SDFatFS_init();
|
||
SDFatFS_Handle sdFatFs_handle = SDFatFS_open(sdspiHandle, DRIVE_NUM);
|
||
if (sdFatFs_handle == NULL)
|
||
{
|
||
while(1);
|
||
}
|
||
|
||
// Чтение из файла
|
||
char readBuffer[READ_BUFFER_SIZE];
|
||
unsigned int bytesRead = 0;
|
||
|
||
//Open inputfile[]
|
||
|
||
fresult = f_open(&src, inputfile, FA_READ);
|
||
if (fresult != FR_OK) {
|
||
// Ошибка открытия файла файла не существует.
|
||
while(1);
|
||
}
|
||
|
||
//Read from src file
|
||
fresult = f_read(&src, readBuffer, sizeof(readBuffer), &bytesRead);
|
||
if (fresult != FR_OK) {
|
||
// Ошибка чтения файла
|
||
while(1);
|
||
}
|
||
|
||
//Close inputfile[]
|
||
fresult = f_close(&src);
|
||
if (fresult != FR_OK) {
|
||
// Ошибка закрытия файла
|
||
while(1);
|
||
}
|
||
ESTOP0;
|
||
fresult = f_getfree(STR(DRIVE_NUM), &freeClusterCount, &(dst.obj.fs));
|
||
if (fresult) {
|
||
while (1);
|
||
}
|
||
|
||
else {
|
||
// Get total sectors and free sectors
|
||
totalSectorCount = ((dst.obj.fs)->n_fatent - 2) * (dst.obj.fs)->csize;
|
||
freeSectorCount = freeClusterCount * (dst.obj.fs)->csize;
|
||
ESTOP0;
|
||
}
|
||
|
||
/* Create a new file object for the file copy */
|
||
fresult = f_open(&dst, outputfile, FA_CREATE_ALWAYS|FA_WRITE);
|
||
if (fresult != FR_OK) {
|
||
while(1);
|
||
}
|
||
/* Write to dst file */
|
||
fresult = f_write(&dst, textarray, 7, &bytesWritten);
|
||
if (fresult != FR_OK) {
|
||
while(1);
|
||
}
|
||
|
||
fresult = f_sync(&dst);
|
||
if (fresult != FR_OK) {
|
||
while(1);
|
||
}
|
||
/* Get the filesize of the source file */
|
||
filesize = f_size(&src);
|
||
|
||
/* Close outputfile[] */
|
||
fresult = f_close(&dst);
|
||
if (fresult != FR_OK) {
|
||
while(1);
|
||
}
|
||
|
||
//
|
||
// Enable Global Interrupt (INTM) and realtime interrupt (DBGM)
|
||
//
|
||
EINT;
|
||
ERTM;
|
||
|
||
|
||
SDFatFS_close(sdFatFs_handle);
|
||
}
|
||
|
||
|
||
int32_t fatfs_getFatTime(void)
|
||
{
|
||
return 0;
|
||
}
|
||
//
|
||
// End File
|
||
//
|