SPI DataFlash Library

Aus ProjectWiki
Version vom 12. April 2015, 05:01 Uhr von Mat (Diskussion | Beiträge) (Created page with "== Overview == This library reads/writes Atmel AT25DF-compatible SPI DataFlash ICs (ex. Spansion S25FL) through either a SW or HW SPI interface. The sample serves also as Firmwar...")
(Unterschied) ← Nächstältere Version | Aktuelle Version (Unterschied) | Nächstjüngere Version → (Unterschied)
Wechseln zu:Navigation, Suche

Overview

This library reads/writes Atmel AT25DF-compatible SPI DataFlash ICs (ex. Spansion S25FL) through either a SW or HW SPI interface. The sample serves also as Firmware for the DataFlash Programmer application to initially write data into the memory. It uploads a list of any type of files, image files are converted on the fly to the uncompressed 16-Bit color BIN format. To access the stored information, an include file with all the neccessary constants (memory position, size, image width & height) is generated.

The library has been developed and tested using an XMega-A1 Xplain board with a Spansion S25FL032P (32Mbit) dataflash mounted on it (U502!).


How to use

Include the library. Set the options above the $include statement, needs the new Submode.

Config Submode = New

Const Flash_port_spi = PORTC
$include "SpiFlash.inc"


Init the DataFlash SPI interface

Sub Flash_init()


Read the 81 Bytes JEDEC RDID information

Sub Flash_get_info(byref Spiflashbuffer() As Byte)


Disable the SPI interface

Sub Flash_stop()


Erase a sector (64Kb block, upper 8 Bit of 24 Bit byte adress). The memory has to be erased (set to &HFF) before writing data.

Sub Flash_sector_erase(byval Sector As Byte)


Erase the whole memory

Sub Flash_bulk_erase()


Writes a page to the memory (256 Bytes) to a specified page adress (upper 16 Bit of 24 Bit byte adress).

Sub Flash_write_page(byval Address As Word , Byref Pagedata() As Byte)


Reads a block of data starting from the 24 Bit adress

Sub Flash_read(byval Address As Dword , Byref Spiflashbuffer() As Byte , Byval Length As Word)


Reads a block of data from the flash memory to an SRAM adress

Sub Flash_readto(byval Sourceaddress As Dword , Byval Destinationaddress As Dword , Byval Length As Dword)


Start an asynchronous read sequence. Data can be read byte-wise from the 24 Bit start adress until Flash_endread() is called.

Sub Flash_beginread(byval Address As Dword)


Reads a Byte (needs Flash_beginread() first)

Function Flash_readbyte() As Byte


Stop a read sequence

Sub Flash_endread()


Options

Electrical connection

SPI DataFlash connection schematic

Sample

$regfile = "xm128a1def.dat"
$crystal = 32000000
$hwstack = 64
$swstack = 64
$framesize = 64
Config Submode = New

Const False = 0
Const True = 1
$include "XMegaPll.inc"

Config Com1 = 115200 , Mode = Asynchroneous , Parity = None , Stopbits = 1 , Databits = 8
Open "COM1:" For Binary As #1

' use the watchdog for communication timeouts
Config Watchdog = 1000


Const Flash_port_ss = Portq                                                     ' Xplained DataFlash SS
Const Flash_pin_ss = 2
Const Flash_enable_statusled = True
$include "SPI-Flash.inc"

Dim Flashbuffer(256) As Byte
Dim Shakedhands As Boolean
Dim Tempbyte As Byte , Tempword As Word , Tempdword As Dword

Flash_init

Do
   ' wait for command
   Inputbin #1 , Tempbyte
   Select Case Tempbyte

   ' Handshake/Exit
   Case &HAA:
      Shakedhands = Not Shakedhands                                             ' some kind of accidental write protection
      Printbin #1 , &HF0                                                        ' ack

   ' erase a sector (address bits 23..16)
   Case &H01:
      Reset Watchdog
      Start Watchdog
      Inputbin #1 , Tempbyte                                                    ' read sector address
      Stop Watchdog
      If Shakedhands = True Then Flash_sector_erase Tempbyte
      Printbin #1 , &HF0

   ' erase whole memory
   Case &H02:
      If Shakedhands = True Then Flash_bulk_erase
      Printbin #1 , &HF0

   ' write a page (256 Bytes, address bits 23..8)
   Case &H03:
      Reset Watchdog
      Start Watchdog
      Inputbin #1 , Tempword                                                    ' read page address
      Inputbin #1 , Flashbuffer(1) ; 256                                        ' read 256 bytes page data
      Stop Watchdog
      Swap Tempword
      If Shakedhands = True Then Flash_write_page Tempword , Flashbuffer(1)
      Printbin #1 , &HF0

   ' read a page (256 Bytes, address bits 23..8)
   Case &H04:
      Reset Watchdog
      Start Watchdog
      Inputbin #1 , Tempword                                                    ' read page address
      Stop Watchdog
      Swap Tempword
      Tempdword = Tempword
      Shift Tempdword , Left , 8
      If Shakedhands = True Then Flash_read Tempdword , Flashbuffer(1) , 256
      Printbin #1 , Flashbuffer(1) ; 256                                        ' write page data

   ' read the device ID data (81 Bytes)
   Case &H05:
      If Shakedhands = True Then Flash_get_info Flashbuffer(1)
      Printbin #1 , Flashbuffer(1) ; 81

   End Select
Loop

DataFlash Programmer

SPI DataFlash Programmer application


This Software is Donationware. <paypal>1</paypal> See here for more informations


Download