# Flash Chip problems

**URL:** <https://forum.arduino.cc/t/flash-chip-problems/952424>\
**Category:** Programming\
**Created:** [January 28, 2022, 10:20am UTC](https://forum.arduino.cc/t/flash-chip-problems/952424 "2022-01-28T10:20:29Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![constation](https://avatars.discourse-cdn.com/v4/letter/c/958977/32.png) [@constation](https://forum.arduino.cc/u/constation)\
**Post date:** [January 28, 2022, 10:20am UTC](https://forum.arduino.cc/t/flash-chip-problems/952424/1 "2022-01-28T10:20:29Z")

</div>

```cpp
#include <SerialFlash.h>
#include <SPI.h>
#include "BMI088.h"

const int cspin = 10; // change this to match the chip select pin on your board
SerialFlashFile file;

struct mystructtype{ // size can be modified depending on what variables you want to log
  float ax;
  
} mystruct;

Bmi088Accel accel(Wire,0x18); // change based on your sensor
Bmi088Gyro gyro(Wire,0x68);
uint32_t prevMillis, currentMillis = 0;
const uint32_t sampleInterval = 1000;
float accel_reading;
void writedata(){
  currentMillis = millis();
  SerialFlash.create( "datafile.txt", 1024 ); // change the size of the text file if you need more space (which you will if you want multiple pieces of data)
  file = SerialFlash.open("datafile.txt");
  
  if ( currentMillis - prevMillis > sampleInterval ) {
    prevMillis = currentMillis;
    accel.readSensor();
    gyro.readSensor();   
    accel_reading = accel.getAccelX_mss();
    mystruct.ax = accel_reading; // whatever function is used to get data
    file.write( (uint8_t *)&mystruct, sizeof(mystruct) );  
  }
}

void readdata(){  
  file = SerialFlash.open("datafile.txt");
  file.read( (uint8_t *)&mystruct, sizeof(mystruct) );
  Serial.println( mystruct.ax );
}

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  SerialFlash.begin(cspin);
  accel.begin();
  gyro.begin();
  
}

void loop() {
  // put your main code here, to run repeatedly:
  writedata();
  readdata();
  delay(1000); // delay so that the serial monitor will be readable
}

```

why is this code not working?  
library - [GitHub - PaulStoffregen/SerialFlash: Library for using SPI Flash memory with a filesystem-like interface](https://github.com/PaulStoffregen/SerialFlash)

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [January 28, 2022, 10:42am UTC](https://forum.arduino.cc/t/flash-chip-problems/952424/2 "2022-01-28T10:42:42Z")

</div>

> [@constation](#):
>
> why is this code not working?

What should it do and what does it actually do ?

---

<div class="post-metadata">

**Author:** ![constation](https://avatars.discourse-cdn.com/v4/letter/c/958977/32.png) [@constation](https://forum.arduino.cc/u/constation)\
**Post date:** [January 28, 2022, 10:43am UTC](https://forum.arduino.cc/t/flash-chip-problems/952424/3 "2022-01-28T10:43:31Z")

</div>

This code should log the readings from my imu to my flash chip, but it only logs a couple pieces of data and then prints 2

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [January 28, 2022, 11:35am UTC](https://forum.arduino.cc/t/flash-chip-problems/952424/4 "2022-01-28T11:35:58Z")

</div>

Your code creates the file and opens it each time writeData() is called rather than appending data to it, and the file is never closed

Is that deliberate ?

---

<div class="post-metadata">

**Author:** ![blh64](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/blh64/32/605986_2.png) [@blh64](https://forum.arduino.cc/u/blh64)\
**Post date:** [January 28, 2022, 12:56pm UTC](https://forum.arduino.cc/t/flash-chip-problems/952424/5 "2022-01-28T12:56:48Z")

</div>

You can also check the return values of those SerialFlash.xxxx() calls. You are most likely getting an error that you are just ignoring.

---

<div class="post-metadata">

**Author:** ![johnwasser](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/johnwasser/32/214270_2.png) [@johnwasser](https://forum.arduino.cc/u/johnwasser)\
**Post date:** [January 28, 2022, 2:43pm UTC](https://forum.arduino.cc/t/flash-chip-problems/952424/6 "2022-01-28T14:43:49Z")

</div>

> Performance oriented design does impose some usage limitations. Files are created with a fixed size which can never change or grow. Once created, files can not be renamed or deleted (except for erasing the entire chip). Files begin with all bytes erased (255). Each byte may be written only once. Files created as erasable may be fully erased, to allow new data to be written.

Looks like you are creating a file of 1024 bytes. That is not a very big log.

When you 'open' an existing file, file.position() is zero and file.size() is the size you specified so there is no easy way to know where the current 'end' of the written file is. If your data can't contain a 0xFF character you could read until you reach one and then file.seek() to the position of the first 0xFF to append new data.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [July 27, 2022, 2:43pm UTC](https://forum.arduino.cc/t/flash-chip-problems/952424/7 "2022-07-27T14:43:54Z")

</div>

Closed on July 27, 2022, 2:43pm UTC

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.
