summaryrefslogtreecommitdiff
path: root/apps/examples/nxffs/nxffs_main.c
blob: 1b50ca3863d085b94bb5984db6ce120ae46bb21b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <nuttx/config.h>

#include <sys/mount.h>

#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <dirent.h>
#include <string.h>
#include <errno.h>
#include <crc32.h>
#include <debug.h>

#include <nuttx/mtd.h>
#include <nuttx/ioctl.h>
#include <nuttx/nxffs.h>

#if CONFIG_RAMMTD_BLOCKSIZE != 32
#  error "SetCONFIG_RAMMTD_BLOCKSIZE to 32"
#endif

static int8_t random_data[1068];
//static int8_t mtd_ram[4096]; <-- Not enough space for two file copies
static int8_t mtd_ram[4096];

static int brake_nxffs(void)
{
  FAR struct mtd_dev_s * mtd_dev;
  int ret,i;
  FILE *config;
  int32_t size;

  mtd_dev = rammtd_initialize(mtd_ram, sizeof(mtd_ram));
  if (mtd_dev == NULL)
    {
      vdbg("RAM MTD of init failed");
    }

  ret = nxffs_initialize(mtd_dev);
  if (ret < 0)
    {
      vdbg("ERROR: NXFFS initialization failed: %d\n", -ret);
      vdbg("errasing it and trying again\n");
      mtd_dev->ioctl(mtd_dev,MTDIOC_BULKERASE,0);
      ret = nxffs_initialize(mtd_dev);
      if(ret <0) return -1; // broke good
    }

  ret = mount(NULL, "/mnt/nxffs", "nxffs", 0, NULL);
  if (ret < 0)
    {
      dbg("ERROR: Failed to mount the NXFFS volume: %d\n", errno);
      dbg("%s\n\r",strerror(errno));
    }

  for(i = 0; i<10; i++)
    {
      config = fopen("/mnt/nxffs/config","w");
      ret = fwrite(random_data,sizeof(random_data),1,config);
      fclose(config);
      if (ret == 1)
        dbg("wrote all the data\r\n");
      else{
        dbg("ERROR! did not write all the data\r\n");
	sleep(1);
	exit(0);
      }
      config = fopen("/mnt/nxffs/config","r");
      size = fread(random_data,sizeof(random_data),1,config);
      if (size == 1)
        dbg("read the right amount of data\n\r");
      else{
	dbg("ERROR! did not read the right amount of data\n\r");
	sleep(1);
	exit(0);
      }
      fclose(config);
    }
  return 0;
}

int user_start(int argc, char *argv[])
{
 return brake_nxffs();
}