Yahoo Groups archive

Lpc2000

Index last updated: 2026-04-28 23:31 UTC

Message

Re: SPI / SD initialization sequence - my way

2005-10-25 by Peter Jakacki

hi Wouter,

That listing is not Forth, just a formatted diagnostic I quickly wrote 
so you can see the traffic. Of course bytes are bytes so it helps to 
know if we are reading or writing them, that's all.

The actual code snippets are;

: CMD    ( data cmd -- res )
    ON SDCS                               \ assert SD CS (in case it 
isn't yet asserted)
    SD@ DROP                              \ dummy read - discard result
    3Fh AND 40h OR SD!                    \ encode cmd and send (leaves 
data to send on stack)
    DUP 18h SHR SD!                       \ send msb of data
    DUP 10h SHR SD!                       \ next byte
    DUP 8 SHR SD!                         \ next byte
    SD!                                   \ send last byte (SD! only 
uses byte)
    95h SD!                               \ dummy checksum (to suit CMD0)
    RES@                                  \ read the result onto the 
data stack
    ;

: InitSdCard    ( -- ocr | false )       \ Initialize SD CARD and return 
with ocr or false
    OFF SDPWR                            \ turn off SD CARD POWER 
(including pulling I/O to VSS)
    5 ms                                 \ 5ms delay
    ON SDPWR                             \ turn on power
    5 ms                                 \ 5ms delay
    200 0 TIMER !                        \ 200ms timeout for fault handling
      BEGIN ?TIMEOUT 0 0 CMD 1 = UNTIL   \ begin sending CMD0(0) until res=1
      BEGIN ?TIMEOUT 0 41 ACMD 0= UNTIL  \ begin sending ACMD41(0) until 
res = 0
      BEGIN 0 58 CMD 0= UNTIL            \ card may be busy - keep 
trying for CMD58(0)
    SD4@                                 \ read 4 bytes of the ocr and 
leave on data stack as 1 word
    GETCID GETCSD                        \ read CID and CSD as well 
(handled by these definitions)
    OFF SDCS                             \ turn off chip select
    ;


*Peter*


armqamp wrote:

>Peter,
>
>been away for a week, but now busy again :)
>Your code looks good only I'm not that familiar with Forth.
>Can you shortly explane what happens when you do a r.. or w.. ? I
>understand that this are the read and write commands, but is it a
>function ore something or does it just writes or reads bytes from the
>SPDR (data register)?
>

Attachments

Move to quarantaine

This moves the raw source file on disk only. The archive index is not changed automatically, so you still need to run a manual refresh afterward.