Revision: 1451 Author: [email protected] Date: Sun Nov 1 20:34:10 2009 Log: moved 23k256 to external\storage\ram\ http://code.google.com/p/jallib/source/detail?r=1451
Added: /trunk/include/external/storage/ram /trunk/include/external/storage/ram/23k256 /trunk/include/external/storage/ram/23k256/sram_23k256.jal Deleted: /trunk/include/external/memory Modified: /trunk/include/external/storage/sd_card/sd_card.jal ======================================= --- /dev/null +++ /trunk/include/external/storage/ram/23k256/sram_23k256.jal Sun Nov 1 20:34:10 2009 @@ -0,0 +1,395 @@ +-- Title: 23K256 sram library +-- Author: Matthew Schinkel - borntechi.com, copyright (c) 2009, all rights reserved. +-- Adapted-by: +-- Compiler: >=2.4l +-- +-- This file is part of jallib (http://jallib.googlecode.com) +-- Released under the ZLIB license (http://www.opensource.org/licenses/zlib-license.html) +-- +-- Description: This is library for 23K256 SPI SRAM, Microchip's 20mhz 256kbit SRAM memory +-- +-- Sources: +-- http://ww1.microchip.com/downloads/en/DeviceDoc/22100D.pdf +-- +-- Functions descriptions: +-- sram_23k256_init - initialize startup settings & spi mode 00 +-- sram_23K256_read_settings - Reads the settings register to get the current 23k256 mode +-- sram_23K256_write - Write one byte to selectd address +-- sram_23K256_read - Read one byte to selectd address +-- sram_23K256_byte[address] - Use as a 32K byte array +-- sram_23K256_word[address] - Use as a 16K word array +-- sram_23K256_dword[address] - Use as a 8K dword array +-- +-- sram_23K256_start_write - get 23K256 ready for writing and +-- select a address to start from (fast writes) +-- sram_23K256_do_write - write byte to the 23K256 +-- sram_23K256_stop_write - tell 23K256 you are finished writing +-- +-- sram_23K256_start_read - get 23K256 ready for reading and +-- select a address to start from (fast reads) +-- sram_23K256_do_read - read byte from the 23K256 +-- sram_23K256_stop_read - tell 23K256 you are finished reading +-- +-- Notes: +-- Address from 0 to 32,767 +-- This is a 2.7v to 3.6v memory. +-- This lib should also work with 23k640 (64kbit sram), but hasn't been tested, please let me know. +-- Also should work with 23A640 & 23A256 (1.5v to 1.95v versions), also not tested +-- SPI Mode is 00 for 23k256 +-- +-- Array Usage: +-- If you are using a procedure that reads/write more then one byte you must +-- set SEQUENTIAL_MODE & must use the chip select pin. +-- +-- Set mode before each read/write. Must be TRUE if more then one component is +-- on bus using different spi modes. +-- +const byte SRAM_23K256_ALWAYS_SET_SPI_MODE = TRUE + +-- constants for use with init procedure input data +-- modes in witch to read and write data +const byte SRAM_23K256_BYTE_MODE = 0b00 -- read only one byte +const byte SRAM_23K256_SEQUENTIAL_MODE = 0b01 -- send read command then read many bytes +const byte SRAM_23K256_PAGE_MODE = 0b10 -- send read command then read 1 32Byte page + +const byte SRAM_23K256_HOLD_DISABLE = 1 -- disable the hold pin. (connect put pin direct to 3.3v) +const byte SRAM_23K256_HOLD_ENABLE = 0 -- enable the hold pin (conect pin to microcontroller) + +-- constants for internal usage +const byte SRAM_23K256_READ_COMMAND = 0b_0000_0011 -- Read data from memory array beginning at selected address +const byte SRAM_23K256_WRITE_COMMAND = 0b_0000_0010 -- Write data to memory array beginning at selected address +const byte SRAM_23K256_READ_STATUS = 0b_0000_0101 -- Read STATUS register +const byte SRAM_23K256_WRITE_SETTINGS = 0b_0000_0001 -- Write STATUS register + + +-- ---------------------------------------------------------------------------- +-- initialize startup settings & spi mode 00 +-- ---------------------------------------------------------------------------- +procedure sram_23k256_init(byte in sram_23k256_mode, byte in sram_23k256_hold_disable) is + -- put spi into the correct mode + SSPCON_CKP = 0 + SSPSTAT_CKE = 1 + + sram_23k236_chip_select = low -- enable the chip + + -- choose the 23k256 read & write mode depending on mode input + if sram_23k256_mode == SRAM_23K256_BYTE_MODE then + spi_master_hw = SRAM_23K256_WRITE_SETTINGS + spi_master_hw = 0x00 + SRAM_23K256_HOLD_DISABLE + end if + if sram_23k256_mode == SRAM_23K256_SEQUENTIAL_MODE then + spi_master_hw = SRAM_23K256_WRITE_SETTINGS + spi_master_hw = 0x40 + SRAM_23K256_HOLD_DISABLE + end if + if sram_23k256_mode == SRAM_23K256_PAGE_MODE then + spi_master_hw = SRAM_23K256_WRITE_SETTINGS + spi_master_hw = 0x80 + SRAM_23K256_HOLD_DISABLE + end if + + sram_23k236_chip_select = high -- disable the chip +end procedure + +-- ---------------------------------------------------------------------------- +-- Reads the status/settings register to get the current 23k256 mode +-- that was previously set in the init procedure. +-- SRAM_23K256_BYTE_MODE, SRAM_23K256_SEQUENTIAL_MODE, SRAM_23K256_PAGE_MODE +-- ---------------------------------------------------------------------------- +procedure sram_23k256_read_settings(byte out mode, byte out hold_is_disabled) is + var byte settings + + -- put spi into mode 00 + if SRAM_23K256_ALWAYS_SET_SPI_MODE == TRUE then + SSPCON_CKP = 0 + SSPSTAT_CKE = 1 + end if + + sram_23k236_chip_select = low -- enable the chip + spi_master_hw = SRAM_23K256_READ_STATUS -- send read status command + settings = spi_master_hw -- get the status byte + sram_23k236_chip_select = high -- disable the chip + + var bit read_hold at settings : 0 -- get bit "hold is disabled" from 23k256 status register byte + hold_is_disabled = read_hold + + var bit mode_bit_0 at settings : 6 -- get 2 bit "mode" from 23k256 status register byte + var bit mode_bit_1 at settings : 7 + + mode = mode_bit_1 -- put 2 bit "mode" into a byte + mode = mode << 1 + mode = mode + mode_bit_0 + +end procedure + +-- ---------------------------------------------------------------------------- +-- Write 1 byte to selected address +-- ---------------------------------------------------------------------------- +procedure sram_23k256_write(word in address, byte in data) is + -- put spi into mode 00 + if SRAM_23K256_ALWAYS_SET_SPI_MODE == TRUE then + SSPCON_CKP = 0 + SSPSTAT_CKE = 1 + end if + + var byte addr[4] at address + + sram_23k236_chip_select = low -- enable the chip + spi_master_hw = SRAM_23K256_WRITE_COMMAND -- send the write command + spi_master_hw = addr[1] -- send the address + spi_master_hw = addr[0] + spi_master_hw = data -- send the data byte + + sram_23k236_chip_select = high -- disable the chip +end procedure + +-- ---------------------------------------------------------------------------- +-- Read 1 byte to selected address +-- ---------------------------------------------------------------------------- +procedure sram_23k256_read(word in address, byte out data) is + -- put spi into mode 00 + if SRAM_23K256_ALWAYS_SET_SPI_MODE == TRUE then + SSPCON_CKP = 0 + SSPSTAT_CKE = 1 + end if + + var byte addr[4] at address + + sram_23k236_chip_select = low -- enable the chip + spi_master_hw = SRAM_23K256_READ_COMMAND -- send the read command + spi_master_hw = addr[1] -- send the address + spi_master_hw = addr[0] + data = spi_master_hw -- get the data + sram_23k236_chip_select = high -- disable the chip +end procedure + +-- ---------------------------------------------------------------------------- +-- Write to 23k256 as a byte array +-- ---------------------------------------------------------------------------- +procedure sram_23k256_byte'put(word in address, byte in data) is + -- put spi into mode 00 + if SRAM_23K256_ALWAYS_SET_SPI_MODE == TRUE then + SSPCON_CKP = 0 + SSPSTAT_CKE = 1 + end if + + var byte addr[4] at address + + sram_23k236_chip_select = low -- enable the chip + spi_master_hw = SRAM_23K256_WRITE_COMMAND -- send the write command + spi_master_hw = addr[1] -- send the address + spi_master_hw = addr[0] + spi_master_hw = data -- send the data byte + + sram_23k236_chip_select = high -- disable the chip +end procedure + +-- ---------------------------------------------------------------------------- +-- Read from 23k256 as a byte array +-- ---------------------------------------------------------------------------- +function sram_23k256_byte'get(word in address) return byte is + var byte data + + -- put spi into mode 00 + if SRAM_23K256_ALWAYS_SET_SPI_MODE == TRUE then + SSPCON_CKP = 0 + SSPSTAT_CKE = 1 + end if + + var byte addr[4] at address + + sram_23k236_chip_select = low -- enable the chip + spi_master_hw = SRAM_23K256_READ_COMMAND -- send the read command + spi_master_hw = addr[1] -- send the address + spi_master_hw = addr[0] + data = spi_master_hw -- get the data + sram_23k236_chip_select = high -- disable the chip + + return data -- return the byte data +end function + +-- ---------------------------------------------------------------------------- +-- Write 23k256 as a large 16K word array +-- ---------------------------------------------------------------------------- +procedure sram_23k256_word'put(word in address, word in data) is + -- put spi into mode 00 + if SRAM_23K256_ALWAYS_SET_SPI_MODE == TRUE then + SSPCON_CKP = 0 + SSPSTAT_CKE = 1 + end if + + address = address * 2 -- multiply by 2 for word address + + var byte full_data[2] at data + + var byte addr[4] at address + + sram_23k236_chip_select = low -- enable the chip + spi_master_hw = SRAM_23K256_WRITE_COMMAND -- send the write command + spi_master_hw = addr[1] -- send the address + spi_master_hw = addr[0] + + spi_master_hw = full_data[1] -- send byte 1 + spi_master_hw = full_data[0] -- send byte 0 + + sram_23k236_chip_select = high -- disable the chip +end procedure + +-- ---------------------------------------------------------------------------- +-- Read from 23k256 as a large 16K word array +-- ---------------------------------------------------------------------------- +function sram_23k256_word'get(word in address) return word is + var word data + + -- put spi into mode 00 + if SRAM_23K256_ALWAYS_SET_SPI_MODE == TRUE then + SSPCON_CKP = 0 + SSPSTAT_CKE = 1 + end if + + address = address * 2 -- multiply by 2 for word address + + var byte full_data[2] at data + + var byte addr[4] at address + + sram_23k236_chip_select = low -- enable the chip + spi_master_hw = SRAM_23K256_READ_COMMAND -- send the read command + spi_master_hw = addr[1] -- send the address + spi_master_hw = addr[0] + + full_data[1] = spi_master_hw -- get byte 1 + full_data[0] = spi_master_hw -- get byte 0 + + sram_23k236_chip_select = high -- disable the chip + + return data -- return the word data +end function + +-- ---------------------------------------------------------------------------- +-- Write 23k256 as a large 8K dword array +-- ---------------------------------------------------------------------------- +procedure sram_23k256_dword'put(word in address, dword in data) is + -- put spi into mode 00 + if SRAM_23K256_ALWAYS_SET_SPI_MODE == TRUE then + SSPCON_CKP = 0 + SSPSTAT_CKE = 1 + end if + + address = address * 4 -- multiply by 4 for dword address + + var byte full_data[4] at data + + var byte addr[4] at address + + sram_23k236_chip_select = low -- enable the chip + spi_master_hw = SRAM_23K256_WRITE_COMMAND -- send the write command + spi_master_hw = addr[1] -- send the address + spi_master_hw = addr[0] + + spi_master_hw = full_data[3] -- send byte 3 + spi_master_hw = full_data[2] -- send byte 2 + spi_master_hw = full_data[1] -- send byte 1 + spi_master_hw = full_data[0] -- send byte 0 + + sram_23k236_chip_select = high -- disable the chip +end procedure + +-- ---------------------------------------------------------------------------- +-- Read from 23k256 as a large 8K dword array +-- ---------------------------------------------------------------------------- +function sram_23k256_dword'get(word in address) return dword is + var dword data + + -- put spi into mode 00 + if SRAM_23K256_ALWAYS_SET_SPI_MODE == TRUE then + SSPCON_CKP = 0 + SSPSTAT_CKE = 1 + end if + + address = address * 4 -- multiply by 4 for dword address + + var byte full_data[4] at data + + var byte addr[4] at address + + sram_23k236_chip_select = low -- enable the chip + spi_master_hw = SRAM_23K256_READ_COMMAND -- send the read command + spi_master_hw = addr[1] -- send the address + spi_master_hw = addr[0] + + full_data[3] = spi_master_hw -- get byte 3 + full_data[2] = spi_master_hw -- get byte 2 + full_data[1] = spi_master_hw -- get byte 1 + full_data[0] = spi_master_hw -- get byte 0 + + sram_23k236_chip_select = high -- disable the chip + + return data -- return the dword data +end function + +-- ---------------------------------------------------------------------------- +-- get 23K256 ready for writing and select a address to start from (fast writes) +-- do not interupt read process by switching to another spi component +-- ---------------------------------------------------------------------------- +procedure sram_23k256_start_write(word in address) is + -- put spi into mode 00 + if SRAM_23K256_ALWAYS_SET_SPI_MODE == TRUE then + SSPCON_CKP = 0 + SSPSTAT_CKE = 1 + end if + + var byte addr[2] at address + + sram_23k236_chip_select = low -- enable the chip + spi_master_hw = SRAM_23K256_WRITE_COMMAND -- send the write command + spi_master_hw = addr[1] -- send the address + spi_master_hw = addr[0] +end procedure + +-- ---------------------------------------------------------------------------- +-- write byte to the 23K256 +-- you may use spi_master_hw directly instead. +-- ---------------------------------------------------------------------------- +procedure sram_23k256_do_write(byte in data) is + spi_master_hw = data -- send the data byte +end procedure + +-- ---------------------------------------------------------------------------- +-- tell 23K256 you are finished writing +-- ---------------------------------------------------------------------------- +procedure sram_23k256_stop_write() is + sram_23k236_chip_select = high -- disable the chip +end procedure + +-- ---------------------------------------------------------------------------- +-- get 23K256 ready for reading and select a address to start from (fast reads) +-- do not interupt read process by switching to another spi component +-- ---------------------------------------------------------------------------- +procedure sram_23k256_start_read(word in address) is + -- put spi into mode 00 + if SRAM_23K256_ALWAYS_SET_SPI_MODE == TRUE then + SSPCON_CKP = 0 + SSPSTAT_CKE = 1 + end if + + var byte addr[2] at address + + sram_23k236_chip_select = low -- enable the chip + spi_master_hw = SRAM_23K256_READ_COMMAND -- send the read command + spi_master_hw = addr[1] -- send the address + spi_master_hw = addr[0] +end procedure + +-- ---------------------------------------------------------------------------- +-- read byte from the 23K256 +-- ---------------------------------------------------------------------------- +procedure sram_23k256_do_read(byte out data) is + data = spi_master_hw -- get the data +end procedure + +-- ---------------------------------------------------------------------------- +-- tell 23K256 you are finished reading +-- ---------------------------------------------------------------------------- +procedure sram_23k256_stop_read() is + sram_23k236_chip_select = high -- disable the chip +end procedure + ======================================= --- /trunk/include/external/storage/sd_card/sd_card.jal Sat Oct 31 23:35:32 2009 +++ /trunk/include/external/storage/sd_card/sd_card.jal Sun Nov 1 20:34:10 2009 @@ -174,6 +174,7 @@ -------------------------------------------------------------------------------- -- tell sd card you will be reading data from a specified sector + -- do not interupt read process by switching to another spi component -------------------------------------------------------------------------------- procedure sd_start_read(dword in address) is -- put spi into mode 11 @@ -236,6 +237,7 @@ -------------------------------------------------------------------------------- -- tell sd card you will be writing data to a specified sector -- must write 1 sector at a time, 512 bytes +-- do not interupt write process by switching to another spi component -------------------------------------------------------------------------------- procedure sd_start_write(dword in address) is -- put spi into mode 11 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "jallib" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/jallib?hl=en -~----------~----~----~----~------~----~------~--~---
