RAM_ROM_Teasts/SD_FAT32/sd_fat32/sd_fat32.c

177 lines
4.0 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
//