How to use sFLASH lib (sst25vf_spi.h)

Hello all,

I am trying to use the sFLASH lib directly but I have no success. This is the code:

SYSTEM_MODE(SEMI_AUTOMATIC);
#define ARRAY_LENGTH 256
#define ADDR 0x80000
static uint8_t g_data[ARRAY_LENGTH];
static uint8_t g_erase[ARRAY_LENGTH];

void fillDat(int w)
{

    for(int i =0; i < ARRAY_LENGTH; i++)
    {
        g_data[i] = (i+w)%256;
        Serial.print(g_data[i]); Serial.print(" ");
    }
    Serial.println();
    sFLASH_WriteBuffer(g_erase, ADDR, ARRAY_LENGTH);
    sFLASH_WriteBuffer(g_data, ADDR, ARRAY_LENGTH);
}

void readback()
{
    sFLASH_ReadBuffer(g_data, ADDR, ARRAY_LENGTH);
    for(int i =0; i < ARRAY_LENGTH; i++)
    {
        Serial.print(g_data[i]); Serial.print(" ");
    }
    Serial.println();
}
void setup()
{
    Serial.begin(9600);
    Spark.connect();

    sFLASH_Init();
    sFLASH_EraseSector(ADDR);
    for(int i =0; i < ARRAY_LENGTH; i++)
        g_erase[i] = 255;
}

int cnt = 1;
void loop()
{
    Serial.print("Test "); Serial.println(cnt);
    delay(3000);
    fillDat(cnt++);
    readback();
}

The expected printout is:

Test 1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 19 20 21 22
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 19 20 21 22

but I get:

Test 1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 19 20 21 22
255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 

Any suggestion what I am doing wrong?
Markus

I doesn’t look like you are writing it. In my code I used a buffer size of 256 and passed 256 as the third arg to sFLASH_WriteBuffer(). Maybe that would help you.

@bko: I changed the array size to 256 and this is the result now:

Test 2
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 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 0 1 
0 2 0 4 4 6 0 8 8 10 8 12 12 14 0 16 16 18 16 20 20 22 16 24 24 26 24 28 28 30 0 32 32 34 32 36 36 38 32 40 40 42 40 44 44 46 32 48 48 50 48 52 52 54 48 56 56 58 56 60 60 62 0 64 64 66 64 68 68 70 64 72 72 74 72 76 76 78 64 80 80 82 80 84 84 86 80 88 88 90 88 92 92 94 64 96 96 98 96 100 100 102 96 104 104 106 104 108 108 110 96 112 112 114 112 116 116 118 112 120 120 122 120 124 124 126 0 128 128 130 128 132 132 134 128 136 136 138 136 140 140 142 128 144 144 146 144 148 148 150 144 152 152 154 152 156 156 158 128 160 160 162 160 164 164 166 160 168 168 170 168 172 172 174 160 176 176 178 176 180 180 182 176 184 184 186 184 188 188 190 128 192 192 194 192 196 196 198 192 200 200 202 200 204 204 206 192 208 208 210 208 212 212 214 208 216 216 218 216 220 220 222 192 224 224 226 224 228 228 230 224 232 232 234 232 236 236 238 224 240 240 242 240 244 244 246 240 248 248 250 248 252 252 254 0 0 

Totally weird order of bytes. You can see only even numbers but not even in the right order.

Hi @MarkusL

FLASH memory needs to be erased before you write it. That puts an all-ones value of 255 into the memory. From that point on you can write locations more than once but you can never write a 1 back into any bit, only change a 1 to a 0.

If you change your test to erase the sector before every write attempt it should work, but be careful that don’t wear out a particular sector by erasing it too much. This is why libraries like @mdma Flashee do wear leveling.

@bko: writing 255 before gets a partial success, the first round is now correct. But when I write 255 a second time it does not seem to have any impact and I get the same strange pattern:

Test 1
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 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 0 
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 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 0 
Test 2
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 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 0 1 
0 2 0 4 4 6 0 8 8 10 8 12 12 14 0 16 16 18 16 20 20 22 16 24 24 26 24 28 28 30 0 32 32 34 32 36 36 38 32 40 40 42 40 44 44 46 32 48 48 50 48 52 52 54 48 56 56 58 56 60 60 62 0 64 64 66 64 68 68 70 64 72 72 74 72 76 76 78 64 80 80 82 80 84 84 86 80 88 88 90 88 92 92 94 64 96 96 98 96 100 100 102 96 104 104 106 104 108 108 110 96 112 112 114 112 116 116 118 112 120 120 122 120 124 124 126 0 128 128 130 128 132 132 134 128 136 136 138 136 140 140 142 128 144 144 146 144 148 148 150 144 152 152 154 152 156 156 158 128 160 160 162 160 164 164 166 160 168 168 170 168 172 172 174 160 176 176 178 176 180 180 182 176 184 184 186 184 188 188 190 128 192 192 194 192 196 196 198 192 200 200 202 200 204 204 206 192 208 208 210 208 212 212 214 208 216 216 218 216 220 220 222 192 224 224 226 224 228 228 230 224 232 232 234 232 236 236 238 224 240 240 242 240 244 244 246 240 248 248 250 248 252 252 254 0 0 

I have tried @mdma flashee lib but it repeatedly crashes in writing after 3 days of operation. To get to the bottom of that problem I am trying to use sFLASH instead.

You don’t write 255, you erase. You are calling sFLASH_EraseSector(addr) in setup, but never again. Erase is a different physical operation on the part than write.

1 Like

OK I see the problem. That means also there is no erase function for a single byte?
How many bytes does a call to sFLASH_EraseSector(ADDR) erases?

The SST25VF devices have a 4K-byte sector erase. So any erase step acts on 4096 bytes starting at the ADDR you pass in, and that ADDR (probably) will be reduced to a modulo-4K number.

1 Like

@MarkusL, have you considered using the flashee-eprom library?

@peekay123 : Yes, I have been using it for some time but I get repeated crashes after ~72 hours of runtime. By using sFLASH I’ll try to narrow down the bug.

Hi @MarkusL

I am really not sure how this going to work for you. Flashee is like a nice easy to drive passenger car while the sFLASH interface is like a vehicle chassis with an engine mounted on it.

They both sort-of do the same thing (and in fact Flashee uses sFLASH) but they are very different to work with.

Hi @bko, like I mentioned at further up I get repeated crashes in flashee (FlashDevice::write) after using it for about 3 days writing 4 bytes per minute. The core resets and 3 days later the same thing. And so on. By eliminating the lib I can narrow my problem down.

@bko: The plain sFLASH code is now running stable for 2 weeks straight which makes me believe that there might be a bug in the Flashee lib that has not been discovered yet.

1 Like

Hi @MarkusL , just an update to say that there was a bug in flashee that was fixed a few months back, in case you want try flashee again should you need the additional wear levelling it provides.

1 Like

Hi @mdma, thanks for the message - I am currently happy with my solution but will use your lib for the next project.