Revision: 1516
Author: [email protected]
Date: Thu Dec  3 22:39:02 2009
Log: added constant to sd_card lib for 10ms delay at stop_write procedure &  
sample.
http://code.google.com/p/jallib/source/detail?r=1516

Modified:
  /trunk/include/external/storage/sd_card/sd_card.jal
  /trunk/include/jal/large_array_2.jal
  /trunk/include/jal/large_array_3.jal
  /trunk/include/jal/large_array_4.jal
  /trunk/sample/16f877_sd_card.jal
  /trunk/sample/18f452_sd_card.jal

=======================================
--- /trunk/include/external/storage/sd_card/sd_card.jal Thu Dec  3 22:06:07  
2009
+++ /trunk/include/external/storage/sd_card/sd_card.jal Thu Dec  3 22:39:02  
2009
@@ -94,7 +94,7 @@
  procedure send_command(byte in command,dword in data, byte out response) is
     var byte paramiters[4] at data
     var byte x
-
+
     -- send a valid CRC byte only for set idle command
     -- right bit must always be 1 (stop bit)
     if command == SD_GO_IDLE_STATE then
@@ -102,11 +102,11 @@
     else
        x = 0xFF
     end if
-
+
     command = command + 64          -- left bits must be 01 (start bits)
-
+
     spi_master_hw = 0xFF            -- send 8 clock pulses
-
+
     spi_master_hw = command         -- send the command
     spi_master_hw = paramiters[3]   -- send command parameters
     spi_master_hw = paramiters[2]   -- send command parameters
@@ -114,13 +114,13 @@
     spi_master_hw = paramiters[0]   -- send command parameters
     spi_master_hw = x               -- CRC data byte, crc disabled in this  
lib.
     spi_master_hw = 0xFF            -- send 8 clock pulses
-
-
+
+
     response = spi_master_hw        -- Get a responce from the card after  
each command
-
-
+
+
     spi_master_hw = 0xFF            -- send 8 clock pulses
-
+
  end procedure

   
--------------------------------------------------------------------------------
@@ -140,21 +140,21 @@
     -- put spi into mode 11
     SSPCON_CKP = 1
     SSPSTAT_CKE = 0
-
+
     var byte reply            -- shows if sd card init is ok
-
+
     -- steps to set sd card to use SPI
     sd_chip_select = low      -- start chip slect low
     sd_chip_select = high     -- chip select high
     for 10 loop
        spi_master_hw = 0xFF    -- send 0xFF 10 times
     end loop
-
+
     sd_chip_select = low      -- enable the sd card
     send_command(SD_GO_IDLE_STATE,0,reply) -- command 0, Resets card to  
idle state, get a response
     sd_ready()             -- wait till sd card is ready
     sd_chip_select = high  -- disable the sd card
-
+
  end procedure

   
--------------------------------------------------------------------------------
@@ -162,13 +162,13 @@
   
--------------------------------------------------------------------------------
  procedure sd_set_idle() is
     sd_chip_select = low  -- enable the sd card
-
+
     var byte reply
     send_command(SD_STOP_TRANSMISSION,0,reply) -- stop current transmission
     sd_ready()                                 -- wait till sd card is ready
     send_command(SD_GO_IDLE_STATE,0,reply)     -- command 0, Resets card to  
idle state
     sd_ready()                                 -- wait till sd card is ready
-
+
     sd_chip_select = high  -- disable the sd card
  end procedure

@@ -182,7 +182,7 @@
        SSPCON_CKP = 1
        SSPSTAT_CKE = 0
     end if
-
+
     var byte reply
     sd_chip_select = low  -- enable the sd card
     address = address * 512 -- make sd card sector addressable, sd cards  
are normally byte addressable.
@@ -197,21 +197,21 @@
   
--------------------------------------------------------------------------------
  procedure sd_read_data(byte out low_byte, byte out high_byte) is
     var byte x
-
+
     if sd_word_count == 0 then              -- beginning of sector read
        while spi_master_hw != 0xFE loop      -- wait till data is ready to  
read
        end loop
     end if
-
+
     low_byte = spi_master_hw                -- get data byte
     high_byte = spi_master_hw               -- get data byte
-
+
     if sd_word_count == 255 then            -- end of sector read
        sd_sector_count = sd_sector_count + 1 -- increment sector number
        x = spi_master_hw                     -- get junk crc data, crc is  
disabled
        x = spi_master_hw                     -- get junk crc data, crc is  
disabled
     end if
-
+
     sd_word_count = sd_word_count + 1       -- increment word_count
  end procedure

@@ -245,7 +245,7 @@
        SSPCON_CKP = 1
        SSPSTAT_CKE = 0
     end if
-
+
     sd_chip_select = LOW  -- enable the sd card
     var byte reply
     address = address * 512 -- make sd card sector addressable, sd cards  
are normally byte addressable.
@@ -259,21 +259,21 @@
   
--------------------------------------------------------------------------------
  procedure sd_write_data(byte in low_byte, byte in high_byte) is
     var byte x
-
+
     if sd_word_count == 0 then  -- beginning of sector write
        spi_master_hw = 0xFC      -- send "stop transmission token" write  
multiple command
        ;spi_master_hw = 0xFE      -- send "stop transmission token" for  
write single command
     end if
-
+
     spi_master_hw = low_byte     -- send data byte
     spi_master_hw = high_byte    -- send data byte
-
+
     if sd_word_count == 255 then -- end of sector write
        sd_sector_count = sd_sector_count + 1 -- increment sector count
-
+
        spi_master_hw = 0xFF  -- send junk crc, crc is disabled
        spi_master_hw = 0xFF  -- send junk crc, crc is disabled
-
+
        x = spi_master_hw   -- wait for a response
        while x == 0 loop
           x = spi_master_hw
@@ -281,15 +281,15 @@
        -- response = 010 = data accepted
        -- response = 101 = crc error
        -- response = 110 = write error
-
+
        x = spi_master_hw   -- wait for sd card to become ready
        while x != 0xFF loop
           x = spi_master_hw
        end loop
     end if
-
+
     sd_word_count = sd_word_count + 1  -- increment word count
-
+
  end procedure

   
--------------------------------------------------------------------------------
@@ -299,7 +299,9 @@
     spi_master_hw = 0xFD -- send "stop transmission token"
     sd_set_idle()
     sd_chip_select = high  -- disable the sd card
-   _usec_delay (10_000)
+   if SD_DELAY_AFTER_WRITE == TRUE then
+      _usec_delay (10_000)
+   end if
  end procedure

   
--------------------------------------------------------------------------------
@@ -324,4 +326,31 @@
     sd_chip_select = high  -- disable the sd card
  end procedure

-
+--------------------------------------------------------------------------------
+-- read one entire sector
+--------------------------------------------------------------------------------
+const dword LARGE_ARRAY_4_SIZE = 512           -- choose number of array  
variables
+const dword LARGE_ARRAY_4_VARIABLE_SIZE = 1    -- choose size of variables  
(byte*2)
+include large_array_4                          -- include the array library
+alias sd_sector_buffer is large_array_4        -- rename/alias the array  
to test
+procedure  sd_read_sector() is
+
+  var byte x
+  var word count1 = 0
+
+  while spi_master_hw != 0xFE loop      -- wait till data is ready to read
+  end loop
+
+  for 512 loop
+    sd_sector_buffer [count1] = spi_master_hw                -- get data  
byte
+    count1 = count1 + 1
+  end loop
+
+  sd_sector_count = sd_sector_count + 1 -- increment sector number
+  x = spi_master_hw                     -- get junk crc data, crc is  
disabled
+  x = spi_master_hw                     -- get junk crc data, crc is  
disabled
+
+  sd_word_count = sd_word_count + 1       -- increment word_count
+
+
+end procedure
=======================================
--- /trunk/include/jal/large_array_2.jal        Thu Dec  3 21:52:37 2009
+++ /trunk/include/jal/large_array_2.jal        Thu Dec  3 22:39:02 2009
@@ -93,7 +93,7 @@
     end if

     -- get a value from a large byte array
-   function large_array_1'get(word in address) return byte is
+   function large_array_2'get(word in address) return byte is
        var byte data

        if LARGE_ARRAY_2_SIZE >  1792 then
@@ -188,7 +188,7 @@
     end function

     -- store a value into a large byte array
-   procedure large_array_1'put (word in address, byte in data) is
+   procedure large_array_2'put (word in address, byte in data) is
        if LARGE_ARRAY_2_SIZE >  1792 then
           if address >=  1792 then
              _large_array_2_byte_8h[address - 1792] = data
@@ -342,7 +342,7 @@
        var  word _large_array_2_word_1a[LARGE_ARRAY_2_SIZE - (128 * 0)]
     end if

-   function large_array_1'get(word in address) return word is
+   function large_array_2'get(word in address) return word is
        var word data

        if LARGE_ARRAY_2_SIZE >  896 then
@@ -438,7 +438,7 @@



-   procedure large_array_1'put (word in address, word in data) is
+   procedure large_array_2'put (word in address, word in data) is
        if LARGE_ARRAY_2_SIZE >  896 then
           if address >=  896 then --
              _large_array_2_word_8h[address - 896] = data
@@ -594,7 +594,7 @@
     end if


-   function large_array_1'get(dword in address) return dword is
+   function large_array_2'get(dword in address) return dword is
        var dword data
        if LARGE_ARRAY_2_SIZE > 448 then
           if address >=  448 then
@@ -687,7 +687,7 @@
     end function


-   procedure large_array_1'put (dword in address, dword in data) is
+   procedure large_array_2'put (dword in address, dword in data) is
        if LARGE_ARRAY_2_SIZE > 448 then
           if address >=  448 then
              _large_array_2_dword_8h[address - 448] = data
@@ -802,7 +802,7 @@
  ;        end if
  ;
  ;        -- get a value from a large byte array
-;        function large_array_1'get(word in address) return byte is
+;        function large_array_2'get(word in address) return byte is
  ;          var byte data
  ;
  ;          -- if "LARGE_ARRAY_2_SIZE > 256" we will use "b" array's. "a"  
arrays will get ignored by compiler.
@@ -820,7 +820,7 @@
  ;        end function
  ;
  ;        -- store a value into a large byte array
-;        procedure large_array_1'put (word in address, byte in data) is
+;        procedure large_array_2'put (word in address, byte in data) is
  ;          -- if "LARGE_ARRAY_2_SIZE > 256" we will use "b" array's. "a"  
arrays will get ignored by compiler.
  ;          if LARGE_ARRAY_2_SIZE >  256 then          -- notice constants  
here,
  ;            if address >=  256 then
=======================================
--- /trunk/include/jal/large_array_3.jal        Thu Dec  3 21:52:37 2009
+++ /trunk/include/jal/large_array_3.jal        Thu Dec  3 22:39:02 2009
@@ -93,7 +93,7 @@
     end if

     -- get a value from a large byte array
-   function large_array_1'get(word in address) return byte is
+   function large_array_3'get(word in address) return byte is
        var byte data

        if LARGE_ARRAY_3_SIZE >  1792 then
@@ -188,7 +188,7 @@
     end function

     -- store a value into a large byte array
-   procedure large_array_1'put (word in address, byte in data) is
+   procedure large_array_3'put (word in address, byte in data) is
        if LARGE_ARRAY_3_SIZE >  1792 then
           if address >=  1792 then
              _large_array_3_byte_8h[address - 1792] = data
@@ -342,7 +342,7 @@
        var  word _large_array_3_word_1a[LARGE_ARRAY_3_SIZE - (128 * 0)]
     end if

-   function large_array_1'get(word in address) return word is
+   function large_array_3'get(word in address) return word is
        var word data

        if LARGE_ARRAY_3_SIZE >  896 then
@@ -438,7 +438,7 @@



-   procedure large_array_1'put (word in address, word in data) is
+   procedure large_array_3'put (word in address, word in data) is
        if LARGE_ARRAY_3_SIZE >  896 then
           if address >=  896 then --
              _large_array_3_word_8h[address - 896] = data
@@ -594,7 +594,7 @@
     end if


-   function large_array_1'get(dword in address) return dword is
+   function large_array_3'get(dword in address) return dword is
        var dword data
        if LARGE_ARRAY_3_SIZE > 448 then
           if address >=  448 then
@@ -687,7 +687,7 @@
     end function


-   procedure large_array_1'put (dword in address, dword in data) is
+   procedure large_array_3'put (dword in address, dword in data) is
        if LARGE_ARRAY_3_SIZE > 448 then
           if address >=  448 then
              _large_array_3_dword_8h[address - 448] = data
@@ -802,7 +802,7 @@
  ;        end if
  ;
  ;        -- get a value from a large byte array
-;        function large_array_1'get(word in address) return byte is
+;        function large_array_3'get(word in address) return byte is
  ;          var byte data
  ;
  ;          -- if "LARGE_ARRAY_3_SIZE > 256" we will use "b" array's. "a"  
arrays will get ignored by compiler.
@@ -820,7 +820,7 @@
  ;        end function
  ;
  ;        -- store a value into a large byte array
-;        procedure large_array_1'put (word in address, byte in data) is
+;        procedure large_array_3'put (word in address, byte in data) is
  ;          -- if "LARGE_ARRAY_3_SIZE > 256" we will use "b" array's. "a"  
arrays will get ignored by compiler.
  ;          if LARGE_ARRAY_3_SIZE >  256 then          -- notice constants  
here,
  ;            if address >=  256 then
=======================================
--- /trunk/include/jal/large_array_4.jal        Thu Dec  3 21:52:37 2009
+++ /trunk/include/jal/large_array_4.jal        Thu Dec  3 22:39:02 2009
@@ -93,7 +93,7 @@
     end if

     -- get a value from a large byte array
-   function large_array_1'get(word in address) return byte is
+   function large_array_4'get(word in address) return byte is
        var byte data

        if LARGE_ARRAY_4_SIZE >  1792 then
@@ -188,7 +188,7 @@
     end function

     -- store a value into a large byte array
-   procedure large_array_1'put (word in address, byte in data) is
+   procedure large_array_4'put (word in address, byte in data) is
        if LARGE_ARRAY_4_SIZE >  1792 then
           if address >=  1792 then
              _large_array_4_byte_8h[address - 1792] = data
@@ -342,7 +342,7 @@
        var  word _large_array_4_word_1a[LARGE_ARRAY_4_SIZE - (128 * 0)]
     end if

-   function large_array_1'get(word in address) return word is
+   function large_array_4'get(word in address) return word is
        var word data

        if LARGE_ARRAY_4_SIZE >  896 then
@@ -438,7 +438,7 @@



-   procedure large_array_1'put (word in address, word in data) is
+   procedure large_array_4'put (word in address, word in data) is
        if LARGE_ARRAY_4_SIZE >  896 then
           if address >=  896 then --
              _large_array_4_word_8h[address - 896] = data
@@ -594,7 +594,7 @@
     end if


-   function large_array_1'get(dword in address) return dword is
+   function large_array_4'get(dword in address) return dword is
        var dword data
        if LARGE_ARRAY_4_SIZE > 448 then
           if address >=  448 then
@@ -687,7 +687,7 @@
     end function


-   procedure large_array_1'put (dword in address, dword in data) is
+   procedure large_array_4'put (dword in address, dword in data) is
        if LARGE_ARRAY_4_SIZE > 448 then
           if address >=  448 then
              _large_array_4_dword_8h[address - 448] = data
@@ -802,7 +802,7 @@
  ;        end if
  ;
  ;        -- get a value from a large byte array
-;        function large_array_1'get(word in address) return byte is
+;        function large_array_4'get(word in address) return byte is
  ;          var byte data
  ;
  ;          -- if "LARGE_ARRAY_4_SIZE > 256" we will use "b" array's. "a"  
arrays will get ignored by compiler.
@@ -820,7 +820,7 @@
  ;        end function
  ;
  ;        -- store a value into a large byte array
-;        procedure large_array_1'put (word in address, byte in data) is
+;        procedure large_array_4'put (word in address, byte in data) is
  ;          -- if "LARGE_ARRAY_4_SIZE > 256" we will use "b" array's. "a"  
arrays will get ignored by compiler.
  ;          if LARGE_ARRAY_4_SIZE >  256 then          -- notice constants  
here,
  ;            if address >=  256 then
=======================================
--- /trunk/sample/16f877_sd_card.jal    Thu Dec  3 22:06:07 2009
+++ /trunk/sample/16f877_sd_card.jal    Thu Dec  3 22:39:02 2009
@@ -14,18 +14,16 @@
  --

  -- compiler dependant device definitions
-;include 16f877
+include 16f877
  ;include 16f877a
-include 18f452
-
+;include 18f452
+--
  pragma target OSC HS               -- HS crystal or resonator
  pragma target clock 20_000_000     -- oscillator frequency
  --
  pragma target WDT  disabled
  pragma target LVP  disabled

-enable_digital_io() -- disable all analog pins if any
-
  -- include the delay library
  include delay

@@ -49,6 +47,7 @@
  spi_init(SPI_MODE_11,SPI_RATE_FOSC_16) -- choose spi mode and speed

  -- setup sd card library
+const bit SD_DELAY_AFTER_WRITE = TRUE -- some
  include sd_card -- include sd card library
  sd_init()       -- initalize the sd card

@@ -60,12 +59,12 @@

  var byte low_byte, high_byte          -- vars for sending and recieving  
data

--- read the boot sector (sector 0)
+-- read the boot sector only (sector 0)
  sd_start_read(0)                      -- get sd card ready for read at  
sector 0
  for 256 loop                          -- read 1 sector (256 words)
-   sd_read_data(low_byte, high_byte)  -- read 2 bytes of data
-   serial_hw_write(low_byte)          -- send byte via serial port
-   serial_hw_write(high_byte)         -- send byte via serial port
+  sd_read_data (low_byte, high_byte)  -- read 2 bytes of data
+  serial_hw_write(low_byte)          -- send byte via serial port
+  serial_hw_write(high_byte)         -- send byte via serial port
  end loop
  sd_stop_read()                        -- tell sd card you are done reading

@@ -75,18 +74,32 @@

  sd_start_write(20)                    -- get sd card ready for write
  for 256 + 128 loop                    -- write 1 sector + 1/2 sector
-   sd_write_data(low_byte, high_byte)  -- write data to the card
+  sd_write_data(low_byte, high_byte)  -- write data to the card
  end loop
  sd_write_to_sector_end(0x00)          -- 2nd sector is not done, so  
finnish it
--- sectors must be completed during write
-
+                                      -- sectors must be completed during  
write
+
  sd_stop_write()                       -- tell sd card you are done writing

  -- read the data back, should get (0x66, 0x22) over and over.
  sd_start_read(20)                     -- get sd card ready for read at  
sector 20
  for 512 loop                          -- read 2 sectors (512 words)
-   sd_read_data (low_byte, high_byte)  -- read 2 bytes of data
-   serial_hw_write (low_byte)          -- send byte via serial port
-   serial_hw_write (high_byte)         -- send byte via serial port
+  sd_read_data(low_byte, high_byte)  -- read 2 bytes of data
+  serial_hw_write(low_byte)          -- send byte via serial port
+  serial_hw_write(high_byte)         -- send byte via serial port
  end loop
  sd_stop_read()                        -- tell sd card you are done reading
+
+---------------
+;-- read one full sector into sd_sector_buffer (512 byte array)
+;sd_start_read(0)                      -- get sd card ready for read at  
sector 0
+;sd_read_sector()
+;sd_stop_read()                        -- tell sd card you are done reading
+;
+;-- now send it to the pc
+;var word count1 = 0
+;for 512 loop                          -- read 1 sector (512 bytes)
+;  serial_hw_write (sd_sector_buffer[count1])          -- send byte via  
serial port
+;  count1 = count1 + 1
+;end loop
+---------------
=======================================
--- /trunk/sample/18f452_sd_card.jal    Thu Dec  3 22:06:07 2009
+++ /trunk/sample/18f452_sd_card.jal    Thu Dec  3 22:39:02 2009
@@ -17,7 +17,7 @@
  ;include 16f877
  ;include 16f877a
  include 18f452
-
+--
  pragma target OSC HS               -- HS crystal or resonator
  pragma target clock 20_000_000     -- oscillator frequency
  --
@@ -47,6 +47,7 @@
  spi_init(SPI_MODE_11,SPI_RATE_FOSC_16) -- choose spi mode and speed

  -- setup sd card library
+const bit SD_DELAY_AFTER_WRITE = TRUE -- some
  include sd_card -- include sd card library
  sd_init()       -- initalize the sd card

@@ -58,12 +59,12 @@

  var byte low_byte, high_byte          -- vars for sending and recieving  
data

--- read the boot sector (sector 0)
+-- read the boot sector only (sector 0)
  sd_start_read(0)                      -- get sd card ready for read at  
sector 0
  for 256 loop                          -- read 1 sector (256 words)
-   sd_read_data (low_byte, high_byte)  -- read 2 bytes of data
-   serial_hw_write (low_byte)          -- send byte via serial port
-   serial_hw_write (high_byte)         -- send byte via serial port
+  sd_read_data (low_byte, high_byte)  -- read 2 bytes of data
+  serial_hw_write(low_byte)          -- send byte via serial port
+  serial_hw_write(high_byte)         -- send byte via serial port
  end loop
  sd_stop_read()                        -- tell sd card you are done reading

@@ -73,18 +74,32 @@

  sd_start_write(20)                    -- get sd card ready for write
  for 256 + 128 loop                    -- write 1 sector + 1/2 sector
-   sd_write_data(low_byte, high_byte)  -- write data to the card
+  sd_write_data(low_byte, high_byte)  -- write data to the card
  end loop
  sd_write_to_sector_end(0x00)          -- 2nd sector is not done, so  
finnish it
--- sectors must be completed during write
-
+                                      -- sectors must be completed during  
write
+
  sd_stop_write()                       -- tell sd card you are done writing

  -- read the data back, should get (0x66, 0x22) over and over.
  sd_start_read(20)                     -- get sd card ready for read at  
sector 20
  for 512 loop                          -- read 2 sectors (512 words)
-   sd_read_data (low_byte, high_byte)  -- read 2 bytes of data
-   serial_hw_write (low_byte)          -- send byte via serial port
-   serial_hw_write (high_byte)         -- send byte via serial port
+  sd_read_data(low_byte, high_byte)  -- read 2 bytes of data
+  serial_hw_write(low_byte)          -- send byte via serial port
+  serial_hw_write(high_byte)         -- send byte via serial port
  end loop
  sd_stop_read()                        -- tell sd card you are done reading
+
+---------------
+;-- read one full sector into sd_sector_buffer (512 byte array)
+;sd_start_read(0)                      -- get sd card ready for read at  
sector 0
+;sd_read_sector()
+;sd_stop_read()                        -- tell sd card you are done reading
+;
+;-- now send it to the pc
+;var word count1 = 0
+;for 512 loop                          -- read 1 sector (512 bytes)
+;  serial_hw_write (sd_sector_buffer[count1])          -- send byte via  
serial port
+;  count1 = count1 + 1
+;end loop
+---------------

--

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.


Reply via email to