'Rabbit_2.bas
'
'Interface with Z-World SF1010 serial flash expansion card (4MB).
'
'This program writes 256 bytes to buffer 1 and then reads them back
'byte by byte using the buffer read mode.  8-bit serial output on MOSI 
'and 8-bit serial input on MOSI uses ShiftIn() and ShiftOut() commands
'which should provide a increase in speed.
'
'SPI Modes 0 and 3 are used (data clocked in on rising edge of CLK, 
'data clocked out on falling edge of CLK). 
'
'copyright, Lisa P. Mickens, Morgan State University, Nov. 2001

Const SPI_CS as Byte = 13		'Definition of BX24 pins
Const SPI_CLK as Byte = 14
Const MOSI as Byte = 15
Const MISO as Byte = 16

Const B1_Write as Byte = bx10000100	'Definition of AT45DB321 commands
Const B1_Read as Byte = bx11010100

Sub Main()
  Dim Address as Integer
  Dim Data as Byte  
  
  Call OpenSerialPort (1, 19200)

  For Address = 0 to 527			'Write data to Buffer 1
    Data = CByte((Address MOD 10) + 2) 
    Call WriteB1 (Address, Data)
  Next

  For Address = 0 to 527			'Read data from Buffer 1
    Data = ReadB1 (Address)
    Call PutB(Data)
    If (((Address+1) MOD 10) <> 0) Then		'Put 10 values to a line
      Call PutByte (Asc(" "))
    Else
      Call NewLine()
    End If
  Next
End Sub

Sub WriteB1 (byVal Address as Integer, ByVal Data as Byte)
  Dim N as Byte, DCare as Byte, Addr_Hi as byte, Addr_Lo as byte
   
  DCare = bx00000000					'Don't Care byte
  Addr_Hi = CByte((CByte(Address\256) AND bx00000011))	'Set up address bytes
  Addr_Lo = CByte (Address MOD 256)
  
  Call PutPin (SPI_CS, 1)	'Bring CS High
  Call PutPin (SPI_CLK, 0)	'Make sure CLK is low
  Call PutPin (MOSI, 0)	'Bring MOSI low
  Call PutPin (SPI_CS,0)	'Bring CS Low to initiate
  
  Call ShiftOut (MOSI, SPI_CLK, 8, B1_Write)	'Send Buffer 1 Write opcode
  Call ShiftOut (MOSI, SPI_CLK, 8, DCare)		'8 Don't care bits			
  Call ShiftOut (MOSI, SPI_CLK, 8, Addr_Hi)	'6 Don't care bits and 2 Address bits
  Call ShiftOut (MOSI, SPI_CLK, 8, Addr_Lo)	'8 Address bits
  Call ShiftOut (MOSI, SPI_CLK, 8, Data)		'Send data byte

  Call PutPin (SPI_CS, 1)	'End session
End Sub

Function ReadB1 (ByVal Address as Integer) as Byte
  Dim DCare as byte, Addr_Hi as byte, Addr_Lo as byte, Val as byte
  
  DCare = bx00000000						'Don't Care byte
  Addr_Hi = CByte((CByte(Address\256) AND bx00000011))	'Set up address bytes
  Addr_Lo = CByte (Address MOD 256)

  Call PutPin (SPI_CS, 1)	'Bring CS High
  Call PutPin (SPI_CS, 0)	'Bring CS Low to initiate
  
  Call ShiftOut (MOSI, SPI_CLK, 8, B1_Read)	'Send Buffer 1 Read opcode
  Call ShiftOut (MOSI, SPI_CLK, 8, DCare)		'Send 8 Don't care bits
  Call ShiftOut (MOSI, SPI_CLK, 8, Addr_Hi)	'Send 6 Don't care bits and 2 Address bits
  Call ShiftOut (MOSI, SPI_CLK, 8, Addr_Lo)	'Send 8 Address bits
  Call ShiftOut (MOSI, SPI_CLK, 8, DCare) 	'Send Don't Care byte 
  Val = ShiftIn (MISO, SPI_CLK, 8)			'Read data byte
  ReadB1 = (Val XOR bx11111111)			'Invert data byte (Rabbit Semi Board does this)
  
  Call PutPin (SPI_CS, 1) 	'Bring CS High to end session
End Function