problem with creating file, fs not declared
I have been trying to save a file with spiffs but the compiler complains about this error
error: 'fs' undeclared
I have these headers
#include "esp_common.h"
#include "gpio.h"
#include "queue.h"
#include "spiffs.h"
#include <stdlib.h>
#include "spiffs_test_params.h"
#include "fcntl.h"
#include "unistd.h"
Anyone knows how to use spiffs or a better way to create or save data?
Sorry, need more info. What processor? ESP? What toolchain? Do you have more code to show? Did you do the integration yourself or not?
Iam using eap8266 under esp-open-sdk
this is my code:
#include "esp_common.h"
#include "hkc.h"
#include "gpio.h"
#include "queue.h"
#include "spiffs.h"
#include <stdlib.h>
#include "spiffs_test_params.h"
#include "fcntl.h"
#include "unistd.h"
xQueueHandle identifyQueue;
struct gpio {
int aid;
int iid;
} gpio2;
void led_task(void *arg) //make transfer of gpio via arg, starting as a static variable in led routine
{
int i,original;
cJSON *value;
os_printf("led_task started\n");
value=cJSON_CreateBool(0); //value doesn't matter
while(1) {
vTaskDelay(1500); //15 sec
original=GPIO_INPUT(GPIO_Pin_2); //get original state
// os_printf("original:%d\n",original);
value->type=original^1;
GPIO_OUTPUT(GPIO_Pin_2,original^1); // and toggle
change_value( gpio2.aid,gpio2.iid,value);
send_events(NULL,gpio2.aid,gpio2.iid);
}
}
static void example_read_file_posix()
{
const int buf_size = 0xFF;
uint8_t buf[buf_size];
int fd = open("test.txt", O_RDONLY);
if (fd < 0) {
printf("Error opening file\n");
return;
}
int read_bytes = read(fd, buf, buf_size);
printf("Read %d bytes\n", read_bytes);
buf[read_bytes] = '\0'; // zero terminate string
printf("Data: %s\n", buf);
close(fd);
}
static void example_read_file_spiffs()
{
const int buf_size = 0xFF;
uint8_t buf[buf_size];
spiffs_file fd = SPIFFS_open(&fs, "other.txt", SPIFFS_RDONLY, 0);
if (fd < 0) {
printf("Error opening file\n");
return;
}
int read_bytes = SPIFFS_read(&fs, fd, buf, buf_size);
printf("Read %d bytes\n", read_bytes);
buf[read_bytes] = '\0'; // zero terminate string
printf("Data: %s\n", buf);
SPIFFS_close(&fs, fd);
}
static void example_write_file()
{
uint8_t buf[] = "Example data, written by ESP8266";
int fd = open("other.txt", O_WRONLY|O_CREAT, 0);
if (fd < 0) {
printf("Error opening file\n");
return;
}
int written = write(fd, buf, sizeof(buf));
printf("Written %d bytes\n", written);
close(fd);
}
static void example_fs_info()
{
uint32_t total, used;
SPIFFS_info(&fs, &total, &used);
printf("Total: %d bytes, used: %d bytes", total, used);
}
void test_task(void *pvParameters)
{
#if SPIFFS_SINGLETON == 1
esp_spiffs_init();
#else
// for run-time configuration when SPIFFS_SINGLETON = 0
esp_spiffs_init(0x200000, 0x10000);
#endif
if (esp_spiffs_mount() != SPIFFS_OK) {
printf("Error mount SPIFFS\n");
}
while (1) {
vTaskDelay(2000 / portTICK_PERIOD_MS);
example_write_file();
example_read_file_posix();
example_read_file_spiffs();
example_fs_info();
printf("\n\n");
}
}
void identify_task(void *arg)
{
int i,original;
os_printf("identify_task started\n");
while(1) {
while(!xQueueReceive(identifyQueue,NULL,10));//wait for a queue item
original=GPIO_INPUT(GPIO_Pin_2); //get original state
for (i=0;i<2;i++) {
GPIO_OUTPUT(GPIO_Pin_2,original^1); // and toggle
vTaskDelay(30); //0.3 sec
GPIO_OUTPUT(GPIO_Pin_2,original^0);
vTaskDelay(30); //0.3 sec
}
}
}
void identify(int aid, int iid, cJSON *value, int mode)
{
switch (mode) {
case 1: { //changed by gui
xQueueSend(identifyQueue,NULL,0);
}break;
case 0: { //init
identifyQueue = xQueueCreate( 1, 0 );
xTaskCreate(identify_task,"identify",256,NULL,2,NULL);
}break;
case 2: { //update
//do nothing
}break;
default: {
//print an error?
}break;
}
}
void vSampleFunction( void ){
F_FILE *pxFile;
char c;
/* Open a file called afile.bin. */
pxFile = f_open( "afile.bin", "r" );
if( pxFile != NULL )
{
os_printf("YES file exists");
/*
* Access the file here using f_read().
*/
/* Close the file when all accesses are complete. */
f_close( pxFile );
} else {
os_printf("No file exists");
}
}
void user_init(void)
{
os_printf("start of user_init @ %d\n",system_get_time()/1000);
xTaskCreate(test_task, "test_task", 1024, NULL, 2, NULL);
os_printf("end of user_init @ %d\n",system_get_time()/1000);
}
uint32 user_rf_cal_sector_set(void) {
extern char flashchip;
SpiFlashChip *flash = (SpiFlashChip*)(&flashchip + 4);
// We know that sector size is 4096
//uint32_t sec_num = flash->chip_size / flash->sector_size;
uint32_t sec_num = flash->chip_size >> 12;
return sec_num - 5;
}
I gather this issue should be under the esp-open-rtos project instead, but I think you're missing
#include "esp_spiffs.h"
I added it but still:
error: 'fs' undeclared (first use in this function)
do you think I can create this variable missing?
As mentioned, I think it would be a lot safer asking this on the esp-open-rtos project. In their spiffs example
it is NOT added, so I would not recommend it. Otherwise it should just be a matter of declaring spiffs fd; I guess.
I'd try getting esp-open-rtos's spiffs example up and running, and then take it from there.
My guess is that you've missed including a library in the make config or something.
Hello , I try do use SPIFFS on my project , I'm using IAR WORKBENCH , but when I compiled my project , some errors for headers files are not found ! exemple for this headers files "fcntl.h" and "unistd.h" I use Windows OS !
Someone can help me ?!