SOLVED - MKR 1310 on-board 2MB SPI Flash problem

Hi!

Is there someone, who has already used the on-board Flash memory on the MKR 1310 with SerialFlash library? Can someone please tell me, what is the correct FlashChipSelect value, or how to access the chip?

I try to use the examples provided with the library, but I had no success so far...

Thx,
G

I have been trying different inputs, but no success yet.
The board stops working if I select 21, 22, 39, 40, 41,42…
Have no idea how to proceed from here…

I can tell you this. The CS pin for the onboard chip has a dedicated pin. It's Arduino pin 32, but better to use the dedicated macro FLASH_CS:

#define FLASH_CS      (32u)

Unfortunately, even with knowing the CS pin, I have not had any luck yet with the SerialFlash library and my MKR WAN 1310. I've also tried using this library:

still with no luck.

I'm going to spend a bit more time on this later today, and perhaps ask for some help from someone more knowledgeable on the subject if I can't figure it out myself. I'll post an update here if I learn anything more.

Hi Pert!

Thx, for the help. Maybe we can figure it out together.. :slight_smile:
Yes, the pin is 32, now I know. (After the post I checked the wiring diagram of the MKR 1310, and it is there in plain sight...)

I had no luck with the SerialFlash library so far - it seems to have a bug in the SerialFlash.begin function, because every time it returns 1, instead of 0. I also found a "bugfix" on github for this issue begin() returns false when chip not detected, fixes #53 · PaulStoffregen/SerialFlash@754e1d0 · GitHub , I changed the SerialFlashChip.cpp accordingly but it did not help.

Now I am thinking of trying other SPI libraries, but they need more work... And programmers are as lazy as... as much as we all know... :slight_smile:
Also I will post a question to the Arduino support staff, maybe they can help us out with a working example or some other information.

G

Hi Pert!

I figured out one thing today, what I missed:
Before we try to access the chip, the LORA radio has to be turned off. Even if it was not initialized in the sketch (the default is on).
to do this:

Accessing the SPI 2MB Flash:
To access the memory on the board you can use the Serial Flash library developed by Paul Stoffregen, but you should avoid to access it while the radio module s active. To achieve this you should follow a simple procedure.
To activate the radio modem you use the LoRamodem.begin(); function and to deactivate it to prepare Flash access you need to reset the module with this function: digitalWrite(LORA_RESET, LOW).
You perform your access to the SPI memory - read or write - enclosing it between SerialFlash.begin() and SerialFlash.end().
It is important to follow this procedure to avoid any access to SPI while there is a radio communication ongoing.

I did not do this before, and the SerialFlash.begin function was not working properly - did not give 0 when it should have.
Since I included digitalWrite(LORA_RESET, LOW); it is able to give back a value.

Unfortunately it is 0 every time... :frowning:

From reading that page, I wasn't sure that line was necessary if you haven't first called LoRamodem.begin(). Regardless, I tried it anyway. SerialFlash.begin() returns 0 for me with or without it. I'm using the SerialFlash library at the tip of the master branch, which includes the fix you linked to in your previous reply.

I still get this output when running the SerialLibrary's RawHardwareTest sketch:

Raw SerialFlash Hardware Test

Read Chip Identification:
  JEDEC ID:     FF FF FF
  Part Nummber: (unknown chip)
  Memory Size:  0 bytes

Tests Failed  :{

The flash chip may be left in an improper state.
You might need to power cycle to return to normal.

Do you get the same results?

Hi @Kaszas. One of our experts at Arduino was kind enough to help me out with this. The problem is that the SerialFlash library uses SPI by default, but the MKR WAN 1310's flash is connected to SPI1. So the fix is to change this line:

if (!SerialFlash.begin(FlashChipSelect)) {

to:

if (!SerialFlash.begin(SPI1, FlashChipSelect)) {

After that, it works perfectly for me.

I don't find any mention of the flash chip being on SPI1 in the documentation. Perhaps you could request that to be added via your support ticket to save other MKR WAN 1310 users from this confusion.

Hi Pert!

Wow, great news! Thank you and please thank the expert too! - although it does not work for me yet…
I made a simple sketch to test it, but it does not work…
Am I missing something?

#include <SerialFlash.h>
#include <SPI.h>
#include <MKRWAN.h>

const int FlashChipSelect = 32;
int FCstate = 8;

void setup() 
{
  digitalWrite(LORA_RESET, LOW);
  Serial.begin(9600);
  while (!Serial) ;
  Serial.println("Serial ok");
  delay(1000);
}

void loop() 
{
  FCstate = SerialFlash.begin(SPI1, FlashChipSelect);
  Serial.println(FCstate);
  if(FCstate == 1)
  {
    Serial.println("It works :)");
    while(1);
  }
  Serial.println("nope. :(");
  delay(1500);
  // SerialFlash.sleep();

}

Can you please check why it is not working for me? I also tried the RawHardwareTest examples (with the LORA turned off) but no luck with them either.

Thx,
G

Add this line to the top of your setup function:

pinMode(LORA_RESET, OUTPUT);

The reason that isn't mentioned on the MKR WAN 1310's "Getting Started" page is because if you have previously called the MKRWAN library's begin function, the pin is already set to output mode. But of course, MKR WAN 1310 users may want to use the flash chip even when they are not using the MKRWAN library, so the tutorial should be updated to give this information.

:slight_smile: :slight_smile:
Thank you!!! I should have recognize, that the pin was not declared as output...

And now:

Serial ok
1
It works :slight_smile:

Thank you very much for the help!

You’re welcome. I’m glad to hear it’s working now. I’ll pass your thanks on to our friend “the expert”. Enjoy!
Per

OK, here is the summary for everyone, who is willing to use the MKR1310 2MB flash:

The address of the memory chip is 32, therefore it has to be declared:
const int FlashChipSelect = 32;

The LORA has to be turned off when you want to use the 2MB flash, so include this to setup():
pinMode(LORA_RESET, OUTPUT); (this is the declaration for the LORA reset pin)

before the SerialFlash initialization:
digitalWrite(LORA_RESET, LOW); (this line will turn off LORA module)

The flash chip is on the SPI1 BUS, NOT on the SPI bus therefore initialize it like this:
SerialFlash.begin(SPI1, FlashChipSelect);

Here is a small test sketch, which should work:

/* This sketch is intended to use with arduino MKR1310 boards only
 *  To test the initialization of the 2MB flash memory
 */



#include <SerialFlash.h>
#include <SPI.h>
#include <MKRWAN.h>

const int FlashChipSelect = 32;   //the 2MB flash CS pin is connected to pin 32

int FCstate = 8;  //to see the state of the function

void setup() {
pinMode(LORA_RESET, OUTPUT);  //LORA reset pin declaration as output
  digitalWrite(LORA_RESET, LOW);  //turn off LORA module
  Serial.begin(9600);
  while (!Serial) ; //wait for serial monitor to connect
  Serial.println("Serial ok");  //just a test to see the sketch has started
  delay(500);
}

void loop() {
  FCstate = SerialFlash.begin(SPI1, FlashChipSelect);   //SerialFlash initialization on SPI1 bus, CS is on 32
  Serial.println(FCstate);  //to see the return of the SerialFlash.begin function on serial
  if(FCstate == 1){
    Serial.println("It works! :)");   //we are happy
    while(1);
  }
  Serial.println("nope, something is wrong :(");    //we are not happy
  delay(1500);
  
}

Enjoy! :slight_smile:

Many thanks to Pert and “the expert”!
G

Thanks for taking the time to summarize your findings Kaszas! I'm sure anyone who is searching for information on using the flash chip on the MKR WAN 1310 and finds this will be very grateful.

Hi Pert!

I also asked the arduino support team to put these informations on the official MKR1310 documentation.

G

Thank you!

Hmmm, this behaviour seems odd to me and rather indicative of a design defect. The intended purpose of the MKR WAN 1310 is to use LoRa. So, having to reset the LoRa modem anytime to want to access the spi flash seems to be a poor design decision.

It appears to me that the spi flash should have been put on the SPI bus instead of SPI1. Or, a different SERCOM port should have been used for the serial LoRa connection.

I think perhaps the idea is to use the SPI1 bus for both the LoRa modem and spi flash. But to do that it will require a firmware update to the modem module so that it configures itself to use its SPI port rather than its serial port. Is there any word yet on when the new MKR WAN library will be available and if making it work on SPI is a priority?

Posted an issue at GitHub: