File write gives -5 after file size reaches cache size

So I've been working with an nRF52832 on a custom board, and it has an external flash, mx25r1635f to be exact, and that is mounted with littleFS to use for data storage. It mounts, reads, writes all fine until I start working with bigger data, then it starts giving me error -5, which is I/O error, but I know my connections should be good and it happens on multiple PCBs. I started messing around with the firmware until I realized that the problem happened at a file size of exaclty 63, but would eventually like maybe 30 or 40 trys manage to write a couple of bytes again. This problem happens even in the sample code for file handling if I put it to append the pattern data to the file. After looking through the code trying to find something that could be related I saw that the cache-size attribute was 64, on a whim I changed to 128, and.. boom the file size could get to 127. So I'm thinking it's related to the cache filling up, but I could not for the life of me find a way to solve that, and not a single soul on the internet had the same problem as far as I can tell. The best solution I could find was to split my data into small chunks of 127bytes, but that's gonna be some confusing code for other people to read and will end up a lot more complex than it should be.

.overlay:

&pinctrl {
    spi1_default: spi1_default {
        group1 {
            psels = <NRF_PSEL(SPIM_SCK, 0, 14)>,
				<NRF_PSEL(SPIM_MOSI, 0, 13)>,
				<NRF_PSEL(SPIM_MISO, 0, 16)>;
        };
    };
	
	spi1_sleep: spi1_sleep {
        group1 {
            psels = <NRF_PSEL(SPIM_SCK, 0, 14)>,
				<NRF_PSEL(SPIM_MOSI, 0, 13)>,
				<NRF_PSEL(SPIM_MISO, 0, 16)>;
				low-power-enable;
        };
    };
}

&spi1 {
	compatible = "nordic,nrf-spi";
	status = "okay";
	cs-gpios = <&gpio0 15 GPIO_ACTIVE_LOW>;
};

/delete-node/ &storage_partition;

/ {
	fstab {
		compatible = "zephyr,fstab";
		lfs1: lfs1 {
			compatible = "zephyr,fstab,littlefs";
			mount-point = "/lfs1";
			partition = <&lfs1_part>;
			automount;
			read-size = <16>;
			prog-size = <16>;
			cache-size = <128>;
			lookahead-size = <32>;
			block-cycles = <512>;
		};
	};
};

&mx25r16 {
	partitions {
		compatible = "fixed-partitions";
		#address-cells = <1>;
		#size-cells = <1>;

		lfs1_part: partition@0 {
			label = "storage";
			reg = <0x00000000 0x1000000>;
		};
	};
};


.dts file for the custom board:
 &spi1 {
	 compatible = "nordic,nrf-spi";
	 status = "okay";
	 pinctrl-0 = <&spi1_default>;
	 pinctrl-1 = <&spi1_sleep>;
	 pinctrl-names = "default", "sleep";
	 mx25r16: mx25r1635f@0 {
		compatible = "jedec,spi-nor";
		reg = <0>;
		spi-max-frequency = <8000000>;
		jedec-id = [c2 28 15];
		size = <DT_SIZE_M(16)>;
	};
 };
 
  &flash0 { 
	 partitions {
		 compatible = "fixed-partitions";
		 #address-cells = <1>;
		 #size-cells = <1>;
 
		 boot_partition: partition@0 {
			 label = "mcuboot";
			 reg = <0x00000000 0xc000>;
		 };
		 slot0_partition: partition@c000 {
			 label = "image-0";
			 reg = <0x0000C000 0x32000>;
		 };
		 slot1_partition: partition@3e000 {
			 label = "image-1";
			 reg = <0x0003E000 0x32000>;
		 };
		 scratch_partition: partition@70000 {
			 label = "image-scratch";
			 reg = <0x00070000 0xa000>;
		 };
		 storage_partition: partition@7a000 {
			 label = "storage";
			 reg = <0x0007a000 0x00006000>;
		 };
	 };
 };

the file is first created and written a small part on startup with the following code:

bool sirros_writeFile(const char * file_name, char* file_content, size_t file_size, off_t file_pos) {
	char caminho_arquivo[MAX_FILE_PATH_LEN];
	struct fs_file_t file;
	int ret;
	size_t written_size = 0;
	snprintf(caminho_arquivo, sizeof(caminho_arquivo), "%s%s", mp->mnt_point, file_name);
	fs_file_t_init(&file);
	ret = fs_open(&file, caminho_arquivo, FS_O_CREATE | FS_O_WRITE);
	if(ret < 0)
		return false;

	if (file_pos != 0)
		fs_seek(&file, file_pos, FS_SEEK_SET);

	if(file_size == 0)
		file_size = strlen(file_content);

	written_size = fs_write(&file, file_content, file_size);
	ret = fs_close(&file);
	return written_size == file_size;
}

.c code for writing the file

bool sirros_appendFile(const char * file_name, char* file_content, size_t file_size) {
	char caminho_arquivo[MAX_FILE_PATH_LEN];
	struct fs_file_t file;
	int ret;
	size_t written_size = 0;
	snprintf(caminho_arquivo, sizeof(caminho_arquivo), "%s%s", mp->mnt_point, file_name);
	fs_file_t_init(&file);
	ret = fs_open(&file, caminho_arquivo, FS_O_CREATE | FS_O_APPEND);
	if(ret < 0)
		return false;

	if(file_size == 0)
		file_size = strlen(file_content);

	ret = fs_sync(&file);
	if(ret < 0)
		return false;
	written_size = fs_write(&file, file_content, file_size);
	ret = fs_close(&file);
	return written_size == file_size;
}

If anyone knows how to solve the problem, I'd be very grateful.

Parents Reply Children
No Data
Related