Page 1 of 2

23LC512 SRAM Problem

Posted: Tue Mar 17, 2020 10:34 am
by serkansatuk
Hello.
I have to use 23LC512 SRAM in a project.
I have tried a few 23LC512 libraries. One of them is attached.
I run the library and the sample code on the arduino nano card without any problems when the CS pin is connected to pin 10.
I make the same connections on the STM32F103C8T6 bluepill card as below, but I could not run it.

23LC512 STM32 board
MOSI ---> A7
MISO ---> A6
SCK ---> A5
CS ---> A4 (I also tried changing the const int SRAM_CS = 10; value for A3, and A2.)

I connected MCP3201 to the same SPI pins and SPI2 pins, it works smoothly. MCP4921 runs smoothly in both SPI1 and SPI2.
But this 23LC512 does not work on STM32 card.

Sample code is below.
Can you help me?

Code: Select all

#include <SPI.h>
#include <SPISRAM.h>

/*
 SRAM   Arduino
 1 CS   10(CS)
 2 SO   12(MISO)
 3 -    -
 4 Vss  GND
 5 SI   11(MOSI)
 6 SCK  13(SCK)
 7 HOLD <-- 100k ohm -- 3.3V
 8 Vcc  3.3V
 */
const int SRAM_CS = 2;

SPISRAM myRAM(SRAM_CS, SPISRAM::BUS_MBits); // CS pin
char buffer[128];

void setup() {
//  pinMode(11, OUTPUT);
//  digitalWrite(11, HIGH);
  pinMode(2, OUTPUT);
  digitalWrite(2, HIGH);
  
  Serial.begin(115200);
  
  SPI.begin();
  myRAM.begin();
  
  Serial.println("Byte write...");
  
  myRAM.write(0,'H');
  myRAM.write(1,'e');
  myRAM.write(2,'l');
  myRAM.write(3,'l');
  myRAM.write(4,'o');
  myRAM.write(5,'-');

  Serial.println("Byte read...");
  Serial.println((char)myRAM.read(0));
  Serial.println((char)myRAM.read(1));
  Serial.println((char)myRAM.read(2));
  Serial.println((char)myRAM.read(3));
  Serial.println((char)myRAM.read(4));
  Serial.println((char)myRAM.read(5));

  Serial.println("\nByte write...");
  myRAM.write(0x7FFC,'W');
  myRAM.write(0x7FFD,'o');
  myRAM.write(0x7FFE,'r');
  myRAM.write(0x7FFF,'l');
  myRAM.write(0x8000,'d');
  myRAM.write(0x8001,'!');
  myRAM.write(0x8002,'!');

  Serial.println("Byte read...");
  Serial.println((char)myRAM.read(0x7FFC));
  Serial.println((char)myRAM.read(0x7FFD));
  Serial.println((char)myRAM.read(0x7FFE));
  Serial.println((char)myRAM.read(0x7FFF));
  Serial.println((char)myRAM.read(0x8000));
  Serial.println((char)myRAM.read(0x8001));
  Serial.println((char)myRAM.read(0x8002));

  Serial.println("\nseq write...");
  int addr = 0x7F00;
  myRAM.write(addr, (byte*)"Hello world!!", sizeof("Hello world!!"));

  Serial.println("seq read...");
  myRAM.read(addr, (byte*)buffer, sizeof(buffer));
  Serial.println( buffer );

}

int err;

void loop()
{
  long err = 0;
  Serial.println("\nRandom read/write...");
  for(int i=0; i < 20; i++){
    int addr = i & 0x7fff;
    byte val = i+1;
    Serial.print( addr, HEX );
    Serial.print( " " );
    Serial.print( val, HEX );
    myRAM.write(addr, val);
    Serial.print( " " );
    Serial.println( myRAM.read(addr), HEX );
    if (val != myRAM.read(addr)) err++;
  }
  Serial.print("error count = ");
  Serial.println(err);
  delay(1000);
}

Re: 23LC512 SRAM Problem

Posted: Tue Mar 17, 2020 11:45 am
by stevestrong
There is no SPI.begin() in this lib.

I attach a corrected version.
With this, you don't have to call SPI.begin() in setup().

Re: 23LC512 SRAM Problem

Posted: Tue Mar 17, 2020 12:35 pm
by serkansatuk
I tried corrected version, but it did not work again. It gave same output.

The interesting thing is that the same sample code with this library works seamlessly on the arduno nano card.

I can run MCP4921 and MCP3201 IC with SPI interface in STM32. I just couldn't run 23LC512.



stevestrong wrote: Tue Mar 17, 2020 11:45 am There is no SPI.begin() in this lib.

I attach a corrected version.
With this, you don't have to call SPI.begin() in setup().

Re: 23LC512 SRAM Problem

Posted: Tue Mar 17, 2020 12:38 pm
by stevestrong
Which core do you use?

please set

Code: Select all

const int SRAM_CS = PA4;

Re: 23LC512 SRAM Problem

Posted: Tue Mar 17, 2020 12:44 pm
by serkansatuk
I made that definition and connected the CS pin to A4 but it still doesn't work. I am using STM32F103C8T6. I connected the VCC pin of the 23LC512 to 3.3v so it could be due to the pins being 3.3v in stm32. The problem continues.


stevestrong wrote: Tue Mar 17, 2020 12:38 pm Which core do you use?

please set

Code: Select all

const int SRAM_CS = PA4;

Re: 23LC512 SRAM Problem

Posted: Tue Mar 17, 2020 12:56 pm
by stevestrong
How do you supply the SRAM chip? From 5V?
You should feed it with 3.3V because the STM32F103 works with 3.3V.

Re: 23LC512 SRAM Problem

Posted: Tue Mar 17, 2020 1:00 pm
by serkansatuk
I tried feeding it with both 5v and 3.3v. Sorry it doesn't work :(


stevestrong wrote: Tue Mar 17, 2020 12:56 pm How do you supply the SRAM chip? From 5V?
You should feed it with 3.3V because the STM32F103 works with 3.3V.

Re: 23LC512 SRAM Problem

Posted: Tue Mar 17, 2020 1:01 pm
by stevestrong
I think you set it up wrongly, because the 23LC512 has 16bits address length (corresponding to BUS_KBits) and not 24 bits (BUS_MBits) as you use it currently.

Re: 23LC512 SRAM Problem

Posted: Tue Mar 17, 2020 1:54 pm
by serkansatuk
But it is working good on arduino board with same code.
I changed it to BUS_KBits but, it did not worked again.
stevestrong wrote: Tue Mar 17, 2020 1:01 pm I think you set it up wrongly, because the 23LC512 has 16bits address length (corresponding to BUS_KBits) and not 24 bits (BUS_MBits) as you use it currently.

Re: 23LC512 SRAM Problem

Posted: Tue Mar 17, 2020 5:20 pm
by stevestrong
I think that the problems derives from the different platform variable lengths.
A long on Arduino (8 bit MCU) is not identical with a long on STM32.

I reworked again and attached the lib to avoid this discrepancy.
Please check.

And you can use now:

Code: Select all

SPISRAM myRAM(PA4, BUS_WIDTH_23LC512); // CS pin