SRAM Overview

Introduction:

Serial SRAM is a standalone volatile memory that provides an easy and inexpensive way to add more RAM to an application.  These are 8-pin, low-power devices.   They are high-performance devices with unlimited endurance and zero write times, making them ideal for applications involving continuous data transfer, buffering, data logging, audio, video, internet, graphics, and other math- and data-intensive functions.  

These devices are available from 64 Kbit up to 1 Mbit in density and support SPI, SDI, and SQI™ bus modes.  

The GCBASIC library only supports SPI bus mode.   The GCBASIC library supports hardware and software SPI — this is controlled via a constant, see below.

To use the SRAM library, simply include the following in your user code.  

This initialises the driver.  

    #define SPISRAM_CS      Porta.2      'Also known as SS, or Slave Select
    #define SPISRAM_SCK     Portc.3      'Also known as CLK
    #define SPISRAM_DO      Portc.5      'Also known as MOSI
    #define SPISRAM_DI      Portc.4      'Also known as MISO

    #define SPISRAM_HARDWARESPI
    #define SPISRAM_TYPE     SRAM_23LC1024

SRAM memory operations.   



The library exposes a set of methods to support use of SRAM memory.

Method

Parameters

Usage

SRAMWrite

address as long, value as byte

A subroutine that requires the address and the value to be written to SRAM.

SRAMRead

address as long, value as byte

A subroutine that requires the address and a variable to be updated with the byte value from the specified SRAM address.

SRAMRead

address as long

A function that requires the address. The function returns a byte value from the specified SRAM address.



The library requires a set of constants to support use of SRAM memory.

Constant

Parameters

Usage

SPISRAM_TYPE

Specifies the type of SRAM.

Requires one of the following constants

SRAM_23LC1024,
SRAM_23LCV1024,
SRAM_23A1024,
SRAM_23LCV512,
SRAM_23LC512,
SRAM_23A512,
SRAM_23K256,
SRAM_23A256,
SRAM_23A640, or
SRAM_23K640

SPISRAM_CS

Specifies the port for the chip select pin.

Required

SPISRAM_SCK

Specifies the port for the SPI clock pin.

Required

SPISRAM_DO

Specifies the port for the SPI data out, or MOSI, pin.

Required

SPISRAM_DI

Specifies the port for the data in, or MISO, pin.

Required

HWSPIMode

Specifies the speed of the SPI communications for hardware SPI only.

Optional, defaults to MASTERFAST.

Options are MASTERSLOW,
MASTER,
MASTERFAST, or
MASTERULTRAFAST for specific AVRs only.

SPISRAM_HARDWARESPI

Instructs the library to use hardware SPI; remove or comment out if you want to use software SPI.

Optional



The library also exposes a constant that is specific to the device.   

This may be useful in the user program.   

This constant is used by the library.   

A user may use this public constant.

Constant

Type

Usage

SPISRAM_CAPACITY

Long value

Used to determine the size of the SRAM device

See Also:

Examples

    #include <uno_mega328p.h>
    #option explicit

   ' USART settings
    #define USART_BAUD_RATE 57600
    #define USART_DELAY 0 ms
    #define USART_BLOCKING
    #define USART_TX_BLOCKING


    'SD card attached to SPI bus as follows:
    '
    'UNO:    MOSI - pin 11, MISO - pin 12, CLK - pin 13, CS - pin 4 (CS pin can be changed) and pin #10 (SS) must be an output
    'Mega:   MOSI - pin 51, MISO - pin 50, CLK - pin 52, CS - pin 4 (CS pin can be changed) and pin #52 (SS) must be an output
    'Leonardo: Connect to hardware SPI via the ICSP header

    #define SPISRAM_CS      DIGITAL_5     'Also known as SS, or Slave Select
    #define SPISRAM_SCK      DIGITAL_13    'Also known as CLK
    #define SPISRAM_DO      DIGITAL_11    'Also known as MOSI
    #define SPISRAM_DI      DIGITAL_12    'Also known as MISO

    #define SPISRAM_HARDWARESPI
    #define SPISRAM_TYPE     SRAM_23LC1024

    #define HWSPIMode MASTERULTRAFAST       'MASTERSLOW | MASTER | MASTERFAST | MASTERULTRAFAST for specific AVRs only. Defaults to MASTERFAST


  '********************************************************************************

  'Main program

    'Wait 2 seconds to open the serial terminal
    wait 2 s

    HSerPrintCRLF 2
    HSerPrint "Writing..."
    HSerPrintCRLF
    For SRAM_location=0 to SPISRAM_CAPACITY - 1
     SRAMWrite ( [long]SRAM_location, SRAM_location and 255 )          ' <<< the SRAMWrite instruction
    Next


    dim spirambyteread as Byte
    spirambyteread = 11
    HSerPrintCRLF 2
    dim SRAM_location as long
    HSerPrint "Reading..."
    HSerPrintCRLF
    For SRAM_location=0 to SPISRAM_CAPACITY - 1
     'choose one....
     'SRAMread ( SRAM_location, spirambyteread )
    'or, as a function
     spirambyteread = SRAMread ( SRAM_location )

     if spirambyteread = ( SRAM_location and 255 ) then
       HSerPrint hex(spirambyteread)
     else
       HSerPrint "**"
     end if
     HSerPrint ":"
    Next
    HSerPrintCRLF
    HSerPrint "Wait..."
    HSerPrintCRLF
    Wait 2 s

    HSerPrint "Rewriting to 0x00 ..."
    HSerPrintCRLF
    For SRAM_location=0 to SPISRAM_CAPACITY - 1
     SRAMWrite ( [long]SRAM_location, 0 )
    Next

    Dim errorcount as long
    errorcount = 0
    For SRAM_location=0 to SPISRAM_CAPACITY - 1
     SRAMRead ( SRAM_location, spirambyteread )
     if spirambyteread <> 0 then
       errorcount++
     end if
    Next
    HSerPrint "Error Count (should be 0) = "
    HSerPrint errorcount
    HSerPrintCRLF
    HSerPrint "End..."
    HSerPrintCRLF
    end

Key line: SRAMWrite ( [long]SRAM_location, SRAM_location and 255 ) — writes one test pattern byte to every address the attached SRAM chip supports, verified by the read-back loop further down.