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

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

/*
* @file main.c
* @author TST
* @version V0.0
* @date 02/08/2012
* @brief SDCard example. This is an example showing how to initialize the device and use it with EFSL
*
*/
/* This file includes all the necessary headers (System, STM32, FreeRTOS, TS_Drivers, TS_Boards, TS_Devices
* and TS_Utils) so the program can work properly */
#include "tsmart.h"
/* Third-party headers */
#include "efsl/inc/efs.h"
/* The embedded filesystem */
static EmbeddedFileSystem efs;
#define MAX_FILE_LEN 12 /* name(8).extension(3)\0 */
#define MAX_DATA_LEN 160
#define TSMART_UART tsmart_uart3
uint8_t okMsg[] = "\r\n\n\0";
uint8_t sdMsg1[] = "Please type filename\r\n\0";
uint8_t sdMsg2[] = "Please introduce data to record\r\n\0";
uint8_t sdMenu[] =
"SD-card Access\r\n"
" 1 - List files\r\n"
" 2 - Read file\r\n"
" 3 - Write file\r\n"
" 4 - Delete file\r\n"
" 5 - EXIT\r\n\n\0";
uint8_t sorryMsg[] = "Sorry, I didn't understand\r\n\n\0";
uint8_t byeMsg[] = "Bye!!!\r\n\n\0";
/*
* Task function. Main process to use SDCard.
*
*/
void vEFSL(void *pvParameters){
File fd;
DirList list;
uint16_t i=0;
static uint8_t name[MAX_FILE_LEN];
static uint8_t data[MAX_DATA_LEN];
static uint8_t buff[MAX_DATA_LEN];
int8_t xReturn;
uint8_t opt;
for (;;) {
opt = -1;
/* Show Menu */
TSMART_UART_Send(&TSMART_UART,sdMenu,strlen(sdMenu),portMAX_DELAY);
/* Receive Selected Option */
memset(buff,0x00,sizeof(buff));
TSMART_UART_Receive(&TSMART_UART,buff,1,portMAX_DELAY);
opt = atoi(buff);
switch (opt) {
case 1:
/* **************************** */
/* List files in root directory */
/* **************************** */
xReturn = ls_openDir(&list, &(efs.myFs), "/");
if (xReturn == -1) {
for(;;);
}
while (ls_getNext(&list) == 0) {
TSMART_UART_Send(&TSMART_UART, list.currentEntry.FileName, 11, portMAX_DELAY);
TSMART_UART_Send(&TSMART_UART, "\r\n", strlen("\r\n") ,portMAX_DELAY);
}
TSMART_UART_Send(&TSMART_UART, "\r\n", strlen("\r\n") ,portMAX_DELAY);
break;
case 2:
/* ********* */
/* Read file */
/* ********* */
/* Ask for File */
TSMART_UART_Send(&TSMART_UART, sdMsg1, strlen(sdMsg1), portMAX_DELAY);
/* Receive File Name */
i=0;
do{
TSMART_UART_Receive(&TSMART_UART,&buff[i],1,portMAX_DELAY);
/* Show eco */
TSMART_UART_Send(&TSMART_UART,&buff[i],1,portMAX_DELAY);
i++;
}while(i < MAX_FILE_LEN && buff[i-1]!= '\r'); /* Exit when Buffer is Full or Carry Return */
/* Show Confirmation Message */
TSMART_UART_Send(&TSMART_UART, okMsg, strlen(okMsg),portMAX_DELAY);
/* Append Null Character to end Name */
strncpy(name, buff, i);
name[i-1] = '\0';
/* Read file */
xReturn = file_fopen(&fd, &(efs.myFs),name, 'r');
if (xReturn < 0) {
for(;;);
}
while (file_read(&fd, 1, buff)) {
TSMART_UART_Send(&TSMART_UART,(char *) buff , 1,portMAX_DELAY);
}
TSMART_UART_Send(&TSMART_UART, "\r\n", strlen("\r\n") ,portMAX_DELAY);
/* Close File */
fs_flushFs(&efs.myFs);
break;
case 3:
/* ********** */
/* Write file */
/* ********** */
/* ASK for File */
TSMART_UART_Send(&TSMART_UART, sdMsg1, strlen(sdMsg1), portMAX_DELAY);
/* Receive File Name */
i=0;
do{
TSMART_UART_Receive(&TSMART_UART,&buff[i],1,portMAX_DELAY);
/* Show eco */
TSMART_UART_Send(&TSMART_UART,&buff[i],1,portMAX_DELAY);
i++;
}while(i < MAX_FILE_LEN && buff[i-1]!= '\r'); /* Exit when Buffer is Full or Carry Return */
/* Show Confirmation Message */
TSMART_UART_Send(&TSMART_UART, okMsg, strlen(okMsg),portMAX_DELAY);
/* Append Null Character to end Name */
strncpy(name, buff, i);
name[i-1] = '\0';
/* ASK for Data */
TSMART_UART_Send(&TSMART_UART, sdMsg2, strlen(sdMsg2), portMAX_DELAY);
/* Receive Data to record in File */
i=0;
do{
TSMART_UART_Receive(&TSMART_UART,&buff[i],1,portMAX_DELAY);
/* Show eco */
TSMART_UART_Send(&TSMART_UART,&buff[i],1,portMAX_DELAY);
i++;
}while(i < MAX_DATA_LEN && buff[i-1]!= '\r'); /* Exit when Buffer is Full or Carry Return */
/* Show Confirmation Message */
TSMART_UART_Send(&TSMART_UART, okMsg, strlen(okMsg),portMAX_DELAY);
/* Create the command packet */
strncpy(data, buff, i);
data[i-1] = '\0';
/* Write file */
xReturn = file_fopen(&fd, &(efs.myFs), name, 'a');
if (xReturn < 0) {
for(;;);
}
file_write(&fd, i, data);
fs_flushFs(&efs.myFs);
break;
case 4:
/* *********** */
/* Delete file */
/* *********** */
/* ASK for File */
TSMART_UART_Send(&TSMART_UART, sdMsg1, strlen(sdMsg1), portMAX_DELAY);
/* Receive File Name */
i=0;
do{
TSMART_UART_Receive(&TSMART_UART,&buff[i],1,portMAX_DELAY);
/* Show eco */
TSMART_UART_Send(&TSMART_UART,&buff[i],1,portMAX_DELAY);
i++;
}while(i < MAX_FILE_LEN && buff[i-1]!= '\r'); /* Exit when Buffer is Full or Carry Return */
/* Show Confirmation Message */
TSMART_UART_Send(&TSMART_UART, okMsg, strlen(okMsg),portMAX_DELAY);
/* Append Null Character to end Name */
strncpy(name, buff, i);
name[i-1] = '\0';
/* Remove file */
rmfile(&(efs.myFs), name);
break;
case 5:
/* Bye answer. */
TSMART_UART_Send(&TSMART_UART, byeMsg, strlen(byeMsg), portMAX_DELAY);
for(;;);
break;
default:
/* Bad answer. Discard data */
TSMART_UART_Send(&TSMART_UART, sorryMsg, strlen(sorryMsg), portMAX_DELAY);
continue;
}
}
}
/*
* @brief init() function
*
* This is the first thing that the user must do for using TSmarT.
* It initializes the specific hardware resources (GPRS, GPS, AI, DIO, MODBUS, MSA...)
* and software resources (queues, mutex, tasks...) for the user application.
*
* The way to fill in this function properly is to initialize first
* hardware resources and after that software resources.
*
* This function must return: TSMART_PASS when every thing is OK or
* TSMART_FAIL when a failure happened.
*
* @return
* @arg TSMART_PASS
* @arg TSMART_FAIL
*/
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[MAX_DATA_LEN];
portBASE_TYPE xReturn;
/* ************************************************************************* */
/* Debug Mode */
/* ************************************************************************* */
/* ************************************************************************* */
/* Initialize resources */
/* ************************************************************************* */
/* UART configuration. */
/* Baud rate */
tsmart_uart_config.baud_rate = 19200;
/* Flow control */
tsmart_uart_config.flow_control = TSMART_UART_FC_NONE;
/* Parity bit */
tsmart_uart_config.parity_bit = TSMART_UART_PARITY_NONE;
/* Word length */
tsmart_uart_config.word_length = TSMART_UART_WORDLENGTH_8B;
/* Stop bit */
tsmart_uart_config.stop_bit = TSMART_UART_STOPBITS_1;
/* Buffer size for our UART */
tsmart_uart_config.rx_buf_sz = 50;
/* Buffer for our UART */
tsmart_uart_config.rx_buf = app_rx_buf;
/* UART DMA */
tsmart_uart_config.use_dma = TSMART_UART_DMA_RX_TX;
/* UART time out */
tsmart_uart_config.use_tim = TSMART_UART_TIM_SW;
/* Initialize UART selected */
TSMART_UART_Init(&TSMART_UART, &tsmart_uart_config);
/* Initialize EFS */
xReturn = efs_init(&efs, 0);
if (xReturn != 0) {
for(;;);
}
/* ************************************************************************* */
/* Application task */
/* ************************************************************************* */
/* Create AI_READ task */
if(xTaskCreate(vEFSL, "EFSL", 256, NULL, 7, NULL) != pdPASS){
return TSMART_FAIL;
}
/* Everything OK */
return TSMART_PASS;
}