'--
'Example code for M25P16 16Mbit (2MByte) serial Flash memory
'Code submitted by MARK3 , Sat Apr 21, 2012
Program_top:
$regfile = "m1284pdef.dat" 'Microprocessor Type
$crystal = 8000000 ' Frequency 8Mhz
$hwstack = 110 ' default use 32 for the hardware stack
$swstack = 110 ' default use 10 for the SW stack
$framesize = 160 ' default use 40 for the frame space
Config Submode = New 'there is no need to DECLARE a sub/function before you call it but....(see helpfile)
'<<< USING M25P16-VMW6TG>>>>
' The M25P16 is an 16Mb (2Mb x 8) serial Flash memory
' The memory can be programmed 1 to 256 bytes at a time using the PAGE PROGRAM
' command. It is organized as 32 sectors, each containing 256 pages. Each page is 256
' bytes wide. Memory can be viewed either as 8,192 pages or as 2,097,152 bytes. The entire
' memory can be erased using the BULK ERASE command, or it can be erased one
' sector at a time using the SECTOR ERASE command.
' Sector = 256 pages * 256 bytes = 65536 --> &H00000000 .......&H0000FFFF
'Memory Map
' Sector Address Start Address End
' 31 &H001F0000 &H001FFFFF
' 30 &H001E0000 &H001EFFFF
' ... ......... ..........
' ... ......... ..........
' 1 &H00010000 &H0001FFFF
' 0 &H00000000 &H0000FFFF
'Hardware connections
Config Porta.1 = Input 'Sw1
Config Porta.2 = Input 'Sw2
Config Porta.3 = Input 'Sw3
Config Porta.4 = Input 'Sw4
Config Porta.5 = Input 'Sw5
Sw1 Alias Pina.1
Sw2 Alias Pina.2
Sw3 Alias Pina.3
Sw4 Alias Pina.4 'Wire Gray
Sw5 Alias Pina.5 'Wire Green
Porta.1 = 1 'Sw1
Porta.2 = 1 'Sw2
Porta.3 = 1 'Sw3
Porta.4 = 1 'Sw4
Porta.5 = 1 'Clock/XBee
Config Portb.0 = Output
Cs_m25p16 Alias Portb.0
Set Cs_m25p16 'De-select (Low active)
'Command Set Codes
Const Write_enable = &H06 'The WRITE ENABLE command sets the write enable latch (WEL) bit.
Const Write_disable = &H04 'The WRITE DISABLE command resets the write enable latch (WEL) bit.
Const Read_identification = &H9F
Const Read_status_register = &H05
Const Write_status_register = &H01
Const Read_data_bytes = &H03
Const Read_data_bytes_higher_speed = &H0B
Const Page_program = &H02
Const Sector_erase = &HD8
Const Bulk_erase = &HC7
Const Deep_power_down = &HB9
Const Release_from_deep_power_down = &HAB
Const Wel = 1 'write enable latch bit
Const Wip = 0 'write in progress bit
Const Srwd = 7 'satus register write protect
Dim Df_status As Byte
Dim I As Word
Dim Df_24_bit_address As Long
Dim Df_array(256) As Byte
Dim Df_page_number As Word
Print "Top of Program"
Print ""
'DF_READ_STATUS <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
Function Df_read_status() As Byte
Local Tempvar As Byte
Tempvar = Read_status_register 'READ STATUS REGISTER
Disable Interrupts
Reset Cs_m25p16 'Chip Select
Spiout Tempvar , 1 'Read Status Register
Spiin Df_read_status , 1
Set Cs_m25p16 'Deselect Chip
Enable Interrupts
End Function
'DF_READ_ID <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
Sub Df_read_id()
Local Tempvar As Byte
Tempvar = Read_identification 'READ IDENTIFICATION
Disable Interrupts
Reset Cs_m25p16 'Chip Select
Spiout Tempvar , 1
Spiin Df_array(1) , 20 'Read 3 Byte
Set Cs_m25p16 'Deselect Chip
Enable Interrupts
End Sub
'DF_DEEP_POWERDOWN <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
Sub Df_deep_powerdown_mode()
Local Tempvar As Byte
Tempvar = Deep_power_down
Disable Interrupts
Reset Cs_m25p16
Spiout Tempvar , 1 ' Deep_power_down
Set Cs_m25p16
Enable Interrupts
Waitus 4
End Sub
'DF_RELEASE <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
Sub Df_release_from_powerdown()
Local Tempvar As Byte
Tempvar = Release_from_deep_power_down
Disable Interrupts
Reset Cs_m25p16
Spiout Tempvar , 1 ' Release_from_deep_power_down
Set Cs_m25p16
Enable Interrupts
Waitus 30
End Sub
'PAGE WRITE <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
Sub Df_page_write(page_number As Word)
Local Tempvar As Byte , I As Word , Templong As Long
Local 24_bit_address_1 As Byte , 24_bit_address_2 As Byte , 24_bit_address_3 As Byte , Y As Word
Templong = Page_number * 256
'24-Bit Address
I = Templong
Y = Highw(templong)
24_bit_address_1 = Low(i) 'Bit 7 ......Bit 0
24_bit_address_2 = High(i) 'Bit 15 ......Bit 8
24_bit_address_3 = Low(y) 'Bit 23 ..... Bit 16
Disable Interrupts
Tempvar = Write_enable
Reset Cs_m25p16
Spiout Tempvar , 1 ' WRITE ENABLE
Set Cs_m25p16
Do
Tempvar = Df_read_status()
Loop Until Tempvar.1 = 1 'Wait until The write enable latch (WEL) is set
Tempvar = Page_program
Reset Cs_m25p16
Spiout Tempvar , 1 ' PAGE PROGRAM
'24-Bit address
Spiout 24_bit_address_3 , 1 'Bit 23 ..... Bit 16
Spiout 24_bit_address_2 , 1 'Bit 15 ......Bit 8
Spiout 24_bit_address_1 , 1 'Bit 7 ......Bit 0
For I = 1 To 256
Spiout Df_array(i) , 1 'Send 256 Byte
Next I
Set Cs_m25p16 'Deselect Chip
Do
Tempvar = Df_read_status()
Loop Until Tempvar.0 = 0 'Wait until WIP is Reset
Enable Interrupts
End Sub
'PAGE READ <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
Sub Df_page_read(page_number As Word)
Local Tempvar As Byte , I As Word , Templong As Long
Local 24_bit_address_1 As Byte , 24_bit_address_2 As Byte , 24_bit_address_3 As Byte , Y As Word
Templong = Page_number * 256
'24-Bit Address
I = Templong
Y = Highw(templong)
24_bit_address_1 = Low(i) 'Bit 7 ......Bit 0
24_bit_address_2 = High(i) 'Bit 15 ......Bit 8
24_bit_address_3 = Low(y) 'Bit 23 ..... Bit 16
Disable Interrupts
Tempvar = &H0B ' FAST Read Data Bytes
Reset Cs_m25p16
Spiout Tempvar , 1
'24-Bit address
Spiout 24_bit_address_3 , 1 'Bit 23 ..... Bit 16
Spiout 24_bit_address_2 , 1 'Bit 15 ......Bit 8
Spiout 24_bit_address_1 , 1 'Bit 7 ......Bit 0
Spiin Tempvar , 1 'DUMMY READ
For I = 1 To 256
Spiin Df_array(i) , 1
Next I
Set Cs_m25p16 'Deselect Chip
Enable Interrupts
End Sub
'DF_SECTOR ERASE <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
Sub Df_sector_erase(page_number As Word) 'The SECTOR ERASE command sets to 1 (FFh) all bits inside the chosen sector.
Local Tempvar As Byte , I As Word , Templong As Long
Local 24_bit_address_1 As Byte , 24_bit_address_2 As Byte , 24_bit_address_3 As Byte , Y As Word
Templong = Page_number * 256
'24-Bit Address
I = Templong 'First two byte
Y = Highw(templong) 'last byte
24_bit_address_1 = Low(i) 'Bit 7 ......Bit 0
24_bit_address_2 = High(i) 'Bit 15 ......Bit 8
24_bit_address_3 = Low(y) 'Bit 23 ..... Bit 16
Disable Interrupts
Tempvar = Write_enable
Reset Cs_m25p16
Spiout Tempvar , 1 ' WRITE ENABLE
Set Cs_m25p16
Do
Tempvar = Df_read_status()
Loop Until Tempvar.wel = 1 'Wait until The write enable latch (WEL) is set
Tempvar = Sector_erase
Reset Cs_m25p16
Spiout Tempvar , 1 ' SECTOR ERASE
'24-Bit address
'Any address inside the sector is a valid address for the SECTOR ERASE command
Spiout 24_bit_address_3 , 1 'Bit 23 ..... Bit 16
Spiout 24_bit_address_2 , 1 'Bit 15 ......Bit 8
Spiout 24_bit_address_1 , 1 'Bit 7 ......Bit 0
Set Cs_m25p16 'Deselect Chip
'The WIP bit is 1 during the self-timed SECTOR ERASE
Do
Tempvar = Df_read_status()
Loop Until Tempvar.0 = 0 'Wait until WIP is Reset
Enable Interrupts
End Sub
'Config SPI
Config Spi = Hard , Interrupt = Off , Data Order = Msb , Master = Yes , Polarity = Low , Phase = 0 , Clockrate = 16 , Noss = 0 , Spiin = 0
Spiinit
Wait 1
Main_program: 'just a label
Print "Main Program"
Enable Interrupts
Df_page_number = 1
Call Df_release_from_powerdown()
Print "Df_release_from_powerdown()"
Call Df_sector_erase(df_page_number) '>>>>> WHILE TESTING
Print "Sector Erased"
Do
If Sw5 = 0 Then
Df_page_number = 1
Elseif Sw5 = 1 Then
Df_page_number = 2
End If
If Sw1 = 0 Then 'READ
Print "Sw1 Pressed READ"
Print "--read from page Nr " ; Df_page_number ; " --"
Print ""
Call Df_page_read(df_page_number) '********** PROGRAM READ **************************
For I = 1 To 256 'Data_len
Print Df_array(i) ; "-" ;
Next
Print ""
Waitms 1000
Elseif Sw2 = 0 Then 'WRITE 1
Print "Sw2 Pressed WRITE DATA"
Print "--Write to page Nr " ; Df_page_number ; " --"
Print ""
For I = 1 To 256
Df_array(i) = 13
Next I
Call Df_page_write(df_page_number) '********** PROGRAM WRITE **************************
Print "--page Nr " ; Df_page_number ; " written--"
Print
Waitms 1000
Elseif Sw3 = 0 Then 'WRITE 2
Print "Sw3 Pressed WRITE DATA"
Print "--Write to page Nr " ; Df_page_number ; " --"
Print ""
For I = 1 To 256
Df_array(i) = 11
Next I
Call Df_page_write(df_page_number)
Print "--page Nr " ; Df_page_number ; " written--"
Print ""
Waitms 1000
Elseif Sw4 = 0 Then
Print "Sw4 Pressed ERASE SECTOR"
Call Df_sector_erase(df_page_number)
Print "Df_sector_erase(df_page_number): " ; Df_page_number
Waitms 1000
End If
Loop
End |