API  2.2
TSmarT Software Library
 All Data Structures Functions Variables Typedefs Enumerations Enumerator Groups Pages
sd_fatfs.c

This is an example showing how to use the SDcard with FATFS.

/*
* @file main.c
* @author TST
* @version V0.0
* @date 07/07/2014
* @brief SDCard example. This is an example showing how to initialize the device and use it with FATFS
*
*/
/* TSmarT header */
#include "tsmart.h"
/* Third-party headers */
#include "fatfs/include/ff.h"
#define UART tsmart_uart1
/* Read a text file and display it */
FATFS FatFs; /* Work area (file system object) for logical drive */
void vFATFS (void *pvParameters){
FIL file; /* File object */
char line[82]; /* Line buffer */
FRESULT resp; /* FatFs return code */
uint32_t count,i;
static uint8_t debug_buffer[40*1024];
sprintf(debug_buffer,"\r\n\r\n\t\t\t\tSDCard FATFS EXAMPLE\r\n\t\t\t\r\n\r\n");
TSMART_UART_Send(&UART, debug_buffer, strlen(debug_buffer), 1000/portTICK_RATE_MS);
/* Register work area to the default drive */
sprintf(debug_buffer,"Mounting File System\r\n");
TSMART_UART_Send(&UART, debug_buffer, strlen(debug_buffer), 1000/portTICK_RATE_MS);
if(f_mount(&FatFs, "", 0) != FR_OK){
for(;;);
}
count = BKP_ReadBackupRegister(BKP_DR1);
if (count == 0){
sprintf(debug_buffer,"Creating File\r\n");
TSMART_UART_Send(&UART, debug_buffer, strlen(debug_buffer), 1000/portTICK_RATE_MS);
if ( f_open(&file, "TST.txt", FA_CREATE_ALWAYS | FA_WRITE ) != FR_OK ) {
for(;;);
}else{
sprintf(debug_buffer,"Writing File\r\n");
TSMART_UART_Send(&UART, debug_buffer, strlen(debug_buffer), 1000/portTICK_RATE_MS);
resp = f_puts("[INFO]\r\n", &file );
if ( resp != EOF ) { resp = f_puts("company =TST\r\n", &file ); }
if ( resp != EOF ) { resp = f_puts("author = TST\r\n", &file ); }
f_close( &file );
}
}
sprintf(debug_buffer,"Opening File\r\n");
TSMART_UART_Send(&UART, debug_buffer, strlen(debug_buffer), 1000/portTICK_RATE_MS);
if ( f_open(&file, "TST.txt", FA_OPEN_EXISTING | FA_READ) != FR_OK ) {
for(;;);
}else{
sprintf(debug_buffer,"Reading File : \r\n");
TSMART_UART_Send(&UART, debug_buffer, strlen(debug_buffer), 1000/portTICK_RATE_MS);
while (f_gets(line, sizeof(line), &file)){
sprintf(debug_buffer,"%s",line);
TSMART_UART_Send(&UART, debug_buffer, strlen(debug_buffer), 1000/portTICK_RATE_MS);
}
f_close( &file );
}
sprintf(debug_buffer,"Append data to File\r\n");
TSMART_UART_Send(&UART, debug_buffer, strlen(debug_buffer), 1000/portTICK_RATE_MS);
if ( f_open(&file, "TST.txt", FA_READ | FA_WRITE) != FR_OK ) {
for(;;);
}else{
sprintf(debug_buffer,"Append Data : %d\r\n", count++);
TSMART_UART_Send(&UART, debug_buffer, strlen(debug_buffer), 1000/portTICK_RATE_MS);
/* Move to end of the file to append data */
i = f_lseek(&file, f_size(&file));
i = f_puts(debug_buffer, &file );
BKP_WriteBackupRegister(BKP_DR1, count);
f_close( &file );
}
/* Time to Remove the SDCard */
for(i=0;i<=10;i++){
vTaskDelay(500/portTICK_RATE_MS);
}
NVIC_SystemReset();
}
/*@brief Initialize resources for application
*
* This function initialize the HW and SW resources.
*
*/
int32_t init() {
/* Variables */
tsmart_uart_config_t tsmart_uart_config;
/* Reception buffer UART. Size must be the same as rx_buf_sz */
static uint8_t app_rx_buf[50];
/* ************************************************************************* */
/* Debug Mode */
/* ************************************************************************* */
/* Uncomment for Debug */
/* ************************************************************************* */
/* Initialize resources */
/* ************************************************************************* */
/* UART configuration. */
tsmart_uart_config.baud_rate = 19200;
tsmart_uart_config.flow_control = TSMART_UART_FC_NONE;
tsmart_uart_config.parity_bit = TSMART_UART_PARITY_NONE;
tsmart_uart_config.word_length = TSMART_UART_WORDLENGTH_8B;
tsmart_uart_config.stop_bit = TSMART_UART_STOPBITS_1;
tsmart_uart_config.rx_buf_sz = 50;
tsmart_uart_config.rx_buf = app_rx_buf;
tsmart_uart_config.use_dma = TSMART_UART_DMA_RX_TX;
tsmart_uart_config.use_tim = TSMART_UART_TIM_SW;
/* Initialize UART. UART 1 is selected */
TSMART_UART_Init(&UART, &tsmart_uart_config);
/* Enable Power and Backup interface clocks */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_BKP | RCC_APB1Periph_PWR, ENABLE);
/* Unlock access protection to Backup and RTC registers */
PWR_BackupAccessCmd(ENABLE);
/* ************************************************************************* */
/* Application task */
/* ************************************************************************* */
if(xTaskCreate(vFATFS, "FATFS", 1024, NULL, 7, NULL) != pdPASS){
return TSMART_FAIL;
}
/* Everything OK */
return TSMART_PASS;
}