' ************************** '' ** SD INTERFACE TRAINER ** '' ** By Jeff Ledger ** '' ************************** '' This module is intended for use with the SD COOKBOOK ENTRY '' This uses FullDuplexSerialPlus to interact with the Propeller '' Data displayed on 30,31 (Programming port) '' Baud rate should be: 57600 '' RR20090331 Modifications by Cluso99 '' Change baudrate from 57600 to 115200 '' Extend start delay to 4 seconds to allow time for the user to get PST running '' Bugfix: Fix TestMountSD '' Add routine to display the SD card file directory CON _clkmode = xtal1 + pll16x _xinfreq = 5_000_000 '' SD Configuration spiDO = 16 ''************************************ spiClk = 17 ''** This configuration option must ** spiDI = 18 ''** be included in your code. ** spiCS = 19 ''************************************ OBJ debug: "Parallax serial terminal" ''Used for interaction with the Propeller sd: "fsrwFemto" ''SD Software used in FemtoBASIC VAR long ioControl[2] ''** Variable required for SD Routines ** byte buff[32] '' buffer for SD PUB Main | r,data debug.startrxtx(31,30,0,115200) '' Open serial communication with PST <=== 115200 (was 57600) waitcnt(clkfreq*4 + cnt) '' delay (so you can get PST active) <=== 4 secs (was 2) debug.str(string("Running the propeller SD training program",13)) ''TestSerial '' Uncomment this line to test your communication setup ''TestMountSD '' Uncomment this line to test your SD setup. ''TestDirectory '' Uncomment this line to display your SD directory TestWrite '' Uncomment this line to write a file to your SD card repeat '' <=== loops here (doesn't close program) PUB TestSerial | counter ''Simple communication test routine ''Send messates stored in the DAT block. counter := 1 repeat Debug.Str(string("This is a line number: ")) Debug.Dec(counter++) Debug.Str(string(13,"PST configuration is working.",13,13)) waitcnt(clkfreq + cnt) PUB TestMountSD ''SD Connection Test Routine sd.start(@ioControl) 'start the SD routines if \sd.mount(spiDO,spiClk,spiDI,spiCS) < 0 '' SD Connection Startup debug.str(string(13,"No SD card present.",13)) ''***************************** debug.str(string("SD card expected on: P")) ''** This SD startup routine ** debug.dec(spiDO) ''** provides a gracefull ** debug.str(string(", P")) ''** exit if something is ** debug.dec(spiClk) ''** not connected properly ** debug.str(string(", P")) ''** or if the card is ** debug.dec(spiDI) ''** unplugged. ** debug.str(string(", P")) ''** ** debug.dec(spiCS) ''** Code adapted from: ** debug.str(string(13,13,"Program halted.",13)) ''** Mike Green ** abort ''***************************** else debug.str(string(13,"Connection to SD correctly established..",13)) sd.unmount sd.stop 'Stop the SD Routines PUB TestWrite | r sd.start(@ioControl) '' ** Start the SD Routines ** '' Send a line of text to the SD debug.str(string(13,"** Sending information to SD",13)) sd.mount(spiDO,spiClk,spiDI,spiCS) '' Mount the SD sd.popen(string("alpha.txt"),"w") '' Open alpha.txt for write sd.str(string("Well, Hello how are you doing.",13,10)) sd.str(string("This is another line of text written to the SD card.",13,10)) sd.str(string("Whoa, this is really cool", 13,10)) sd.pclose '' Close the file sd.unmount '' Unmount the SD '' Read the data back to the terminal debug.str(string(13,"** Reading information back from SD",13)) sd.mount(spiDO,spiClk,spiDI,spiCS) '' Mount the SD r := sd.popen(string("alpha.txt"),"r") '' Open alpha.txt for read repeat until r == -1 '' Start a loop until the end of the file is reached. r := sd.pgetc '' Grap a single character if r > 0 '' If character is greater than nothing, display it. debug.char(r) sd.unmount '' Unmount the SD sd.stop 'Stop the SD Routines debug.str(string(13,"** Routine Finished",13)) PUB TestDirectory | i, r ''Display the file directory sd.start(@ioControl) 'start the SD routines check_error(r) sd.mount(spiDO,spiClk,spiDI,spiCS) 'mount the SD check_error(r) debug.str(string(13,"Reading directory",13)) r := sd.opendir 'open directory check_error(r) repeat r := sd.nextfile(@buff) 'get file directory if r == 0 repeat i from 0 to 11 'display filename if buff[i] <> 0 debug.char(buff[i]) debug.char(13) while r == 0 'eof? (end of directory) debug.str(string(13,"Directory complete",13)) sd.unmount 'Unmount the SD sd.stop 'Stop the SD Routines PRI Check_Error(r) if r == -1 debug.str(string("***Error***",13)) sd.unmount sd.stop debug.str(string("***HALTED***",13)) repeat '<============== LOOP HERE ON ERROR