To use this file copy and paste this:
// #URL-lib "http://pin1.org/forthlib/flb/SD-Card/MMC.flb" into BV Terminal 3
or here to download.
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
// Low level access for MMC and SD cards. Provides basic
// reading and writing to card using physical addresses
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
// REQUIRES:
// #URL-lib "http://pin1.org/forthlib/flb/General/soft1.flb"
// #URL-lib "http://pin1.org/forthlib/flb/General/pinsel.flb"
// #URL-lib "http://pin1.org/forthlib/flb/General/SPI.flb"
// CONSTANTS:
integer cspin // used for chip select
integer inital // indicator for initalisation of card
Full Contents of File
integer  cspin     
integer  inital   
: cs  cspin  p!  ;   
: reset1  cs  20  for  &ff  spi  drop  next  ;     
: init
        cspin  io-out       
        spi.init
        reset           
;
: result
        0 
        500  for
                &ff  spi  &fe  =  if  drop  -1  leave  then
        next       
;
: cmd
        &ff  spi  drop       
        &40  or  spi  drop 
        dup  24  rshift  &ff  and  spi  drop 
        dup  16  rshift  &ff  and  spi  drop 
        dup    8  rshift  &ff  and  spi  drop 
        &ff  and  spi  drop 
        &95  spi  drop       
        &ff  spi  drop
        &ff  spi                 
;
: cmd2cmd  &ff  spi  ;   
: blocklen
        0  cs 
        16  cmd  drop
        1  cs
;
: start
        init
        0  cs                       
        0                             
        0  0  cmd  1  =       
        if
                500  for
                        1  ms             
                        0  1  cmd  0=
                        if  i  +  leave  then
                next
        then                       
        1  cs                       
        dup  0  >  if  512  blocklen  then 
;
: id.
        0  cs
        0  10  cmd  drop
        result
        if
                16  for
                        &ff  spi  ph.
                next
                cr
        else
                ."  Cant  initialise  card"               
        then
        1  cs
;
               
: readblock
        0  cs
        17  cmd  drop         
        result                   
        if
                over  +  swap    do 
                        &ff  spi  i  c!
                loop
                2  for  &ff  spi  drop  next         
                -1
        else
                2drop  0               
        then
        1  cs
; 
: writeblock
        >r           
        0  cs
        24  cmd  0=       
        if
                &fe  spi  drop               
                r>  over  +  swap
                do
                        i  c@  spi  drop
                next
                2  for  &ff  spi  drop  next     
                5000  for  &ff  spi  &ff  =  if  leave  then  next         
                leave?  -1  *     
                1  cs
        else
                r>  drop     
                1  cs
                drop       
                0
                ."  Problem  with  MMC  Write"
        then               
; 
: <0>mmc.cs  =>  cspin  ;     
: <0>mmc.start  =>  cspin  start  ;       
: <0>mmc.id.  id.  ;   
: <0>mmc.read  readblock  ;
: <0>mmc.write  writeblock  ;