Example: Write / Read to IPS6404 PSRAM (8 MB)

Post your cool example code here.
Post Reply
User avatar
Vassilis
Posts: 149
Joined: Wed Feb 27, 2019 5:09 pm
Answers: 2
Location: Thessaloniki, Greece
OS: Linux, Win10, MacOS
IDE: Arduino 1.8.9
Core: Roger, STM official
Board: Bluepill, Maple mini, STM32F4xx
Contact:

Example: Write / Read to IPS6404 PSRAM (8 MB)

Post by Vassilis » Mon May 06, 2019 4:03 pm

Write/Read to IPS6404 PSRAM - 8 MB (8 MegaBytes Pseudo Static RAM)

Source code example:

Code: Select all

/**
 * 
 * IPS6404 8 MB PSRAM (SPI SRAM) Write/Read example
 * 
 * (c) 2019 by Vassilis Serasidis <avrsite@yahoo.gr>
 *   Home: https://www.serasidis.gr
 *  Forum: https://mcu.selfip.com , https://www.stm32duino.com
 * 
 * Target: STM32F103C8 (Bluepill) at 72 MHz
 *   Core: STM official (https://github.com/stm32duino/Arduino_Core_STM32)
 *    IDE: Arduino 1.8.9
 */

#include <SPI.h>

#define PSRAM_CS  	PA4
#define BUFFER_LENGTH	8192
#define WRITE_TO_PSRAM	0x02
#define READ_FROM_PSRAM	0x03

uint32_t tmr;
uint8_t tmp_buffer[BUFFER_LENGTH];

/***************************************************
 * 
 **************************************************/
void setup() {
  pinMode(PSRAM_CS, OUTPUT);
  digitalWrite(PSRAM_CS, HIGH);
  
  Serial.begin(9600);
  SPI.begin();
  SPI.beginTransaction(SPISettings(72000000, MSBFIRST, SPI_MODE0));
  
  psram_reset();  //Reset the PSRAM chip
}


/***************************************************
 * 
 **************************************************/
void loop() {

  psram_read_id();

  /* Fill the temporary buffer with data */
  for(uint32_t i=0;i<BUFFER_LENGTH;i++){
    tmp_buffer[i] = i;
  }
  
  /* Write 8192 byte buffer to PSRAM */
  Serial.print("Writing ");
  Serial.print(BUFFER_LENGTH);
  Serial.print(" bytes   to PSRAM (");
  tmr = millis();
  psram_data(WRITE_TO_PSRAM, 0, tmp_buffer, BUFFER_LENGTH); //Write <tmp_buffer> to PSRAM
  tmr = (millis() - tmr);
  Serial.print(tmr);
  Serial.println(" ms)");

  /* Clear temporary buffer */
  for(uint32_t i=0;i<BUFFER_LENGTH;i++){
    tmp_buffer[i] = 0xff;
  }
  
  /* Read 8192 bytes from PSRAM */
  Serial.print("Reading "); 
  Serial.print(BUFFER_LENGTH);
  Serial.print(" bytes from PSRAM (");
  tmr = millis();
  psram_data(READ_FROM_PSRAM, 0, tmp_buffer, BUFFER_LENGTH); //Read data from PSRAM to <tmp_buffer>
  tmr = (millis() - tmr);
  Serial.print(tmr);
  Serial.println(" ms)");
  
  for(uint32_t i=0;i<BUFFER_LENGTH;i++){
    if((i%16) == 0){  //Change line every 16 bytes
      Serial.println();
    }

    if(tmp_buffer[i] < 0x10){ //Print an ASCII zero in case the value is lower than 0x10 
      Serial.write('0');
    }
    Serial.print(tmp_buffer[i], HEX);
    Serial.write(' ');
  }
  
  Serial.println("\n\nComplete !\n\n");
  
  delay(1000);
}


/***************************************************
 * 
 **************************************************/
uint8_t psram_read_id(){

  uint8_t data;
  
  digitalWrite(PSRAM_CS, LOW);
  SPI.transfer(0x9F);

  Serial.print("ID: ");
  for(int i=0;i<6;i++){
    data = SPI.transfer(0xff);
    if(data < 0x10){
      Serial.write('0');
    }
    Serial.print(data, HEX);
    Serial.write(' ');
  }
  digitalWrite(PSRAM_CS, HIGH);
  Serial.println();
}


/***************************************************
 * 
 **************************************************/
void psram_reset(){
  digitalWrite(PSRAM_CS, LOW);
  SPI.transfer(0x66);
  digitalWrite(PSRAM_CS, HIGH);
  
  digitalWrite(PSRAM_CS, LOW);
  SPI.transfer(0x99);
  digitalWrite(PSRAM_CS, HIGH);
}

/***************************************************
 * Read or Write <len> bytes from/to PSRAM
 **************************************************/
void psram_data(uint8_t cmd, uint32_t address, uint8_t *buf, uint32_t len){

  uint8_t data;
  
  digitalWrite(PSRAM_CS, LOW);
  SPI.transfer(cmd);

  SPI.transfer((uint8_t)((address >> 16) & 0xFF));  //Address [23:16]
  SPI.transfer((uint8_t)((address >> 8) & 0xFF));   //Address [15:8]
  SPI.transfer((uint8_t)(address & 0xFF));          //Address [7:0]
  SPI.transfer(buf, len);
  
  digitalWrite(PSRAM_CS, HIGH);
}

Output:

Code: Select all

ID: FF FF FE 0D 5D 46 
Writing 8192 bytes   to PSRAM (11 ms)
Reading 8192 bytes from PSRAM (11 ms)

00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F 
10 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D 1E 1F 
20 21 22 23 24 25 26 27 28 29 2A 2B 2C 2D 2E 2F 
30 31 32 33 34 35 36 37 38 39 3A 3B 3C 3D 3E 3F 
40 41 42 43 44 45 46 47 48 49 4A 4B 4C 4D 4E 4F 
...
...
D0 D1 D2 D3 D4 D5 D6 D7 D8 D9 DA DB DC DD DE DF 
E0 E1 E2 E3 E4 E5 E6 E7 E8 E9 EA EB EC ED EE EF 
F0 F1 F2 F3 F4 F5 F6 F7 F8 F9 FA FB FC FD FE FF 

Complete !

-Vassilis Serasidis

User avatar
mack
Posts: 37
Joined: Fri Mar 08, 2019 7:59 pm
Location: Australia
OS: Windows 10
IDE: Arduino 188
Core: Roger (BluePill),StevStrong (F407 Black)
Board: BluePill,F407 Black

Re: Example: Write / Read to IPS6404 PSRAM (8 MB)

Post by mack » Thu Aug 15, 2019 9:26 pm

Thanks for this snippet Vassilis, I didn't know of this part and I'll be trying it out soon. :D

Cheers
Andrew

Post Reply