'Projektname:       SD_card_Conrad_bewerkt.cprj
'Software Ccontrol 2.13.0.14
'Bentigte Libss:
'Routinen:
'Autor:             Original Conrad, bearbeitet durch Kees Baltus
'Datum:
'Funktion:          Auslesen und schreiben von SD-card mit Arduino SD-slot
'Neue Funktionen:
'Notitz:            Functioniert nicht

'--------------------------------------------------------------------------
' Hardware C.W.A. Baltus:
' Application board met C-Control Mega 128
' Programmierung via USB
' I2C Ein/Ausgang an PD0 und PD1
' Standard Folienkeypad und LCD
' Arduino SC-slot mit Sandisk 2GB card

' Arduino SD-card an:
' MISO -> PB.3   SPI - Schnittstelle
' MOSI -> PB.2   SPI - Schnittstelle
' SCK  -> PB.1   SPI - Schnittstelle
' SS   -> PA.5   SPI - Schnittstelle (abweichend von Conrad standard Anschluss)
' +5V
' GND

'Nicht benutzt:
' WP   -> PE.5   WP (Schreibschutz) high = SD-Karte schreibgeschtzt / low = schreiben erlaubt
' CD   -> PB.4   CD (Kartenerkennung) high = keine SD-Karte erkannt / low = SD-Karte erkannt
' EN1  -> PB.5   En1 = Reset der SD-Karte (low = running mode / high = reset)
' LED  -> PB.7   LED -> Benutzer Led (5V Pegel)
' EN2  -> PB.6   En2 = Versorgung SD-Kartenhalter (low = off / high = on)

'=========================================================================
'Conrad SD-Card Interface
'WP   -> PE.5   WP (Schreibschutz) high = SD-Karte schreibgeschtzt / low = schreiben erlaubt
'CD   -> PB.4   CD (Kartenerkennung) high = keine SD-Karte erkannt / low = SD-Karte erkannt
'MISO -> PB.3   SPI - Schnittstelle
'MOSI -> PB.2   SPI - Schnittstelle
'SCK  -> PB.1   SPI - Schnittstelle
'SS   -> PB.0   SPI - Schnittstelle
'EN1  -> PB.5   En1 = Reset der SD-Karte (low = running mode / high = reset)
'LED  -> PB.7   LED -> Benutzer Led (5V Pegel)
'EN2  -> PB.6   En2 = Versorgung SD-Kartenhalter (low = off / high = on)

'Die SD-Karte darf nicht grer als 2GB sein und sie muss mit FAT (FAT16) formatiert werden.
'Nicht mit FAT32 oder NTFS!
'=========================================================================*/

'Global variables
'Example from Help function in Control software

Dim fat(562) As Byte
Dim fil(32) As Byte
Dim status As Byte
Dim loc, pos As Integer
Dim br, btr As Word

Sub main()
    'Local variables
    'Dim res As Byte
    Dim res As Byte
    Dim buf(100) As Char
    Dim bytes_written(1) As Word
    Dim SD_Card_fileinfo(4) As ULong

    'SD-Card reset
    Port_DataDirBit(13,1)          '  PB.5 = output (EN1)
    Port_DataDirBit(14,1)          '  PB.6 = Ausgang (EN2)
    Port_WriteBit(13,1)            '  set EN1 For 50ms at +5V (PB.5)
    Port_WriteBit(14,0)            '  set EN2 For 50ms To GND (PB.6)

    AbsDelay(50)                   '  50ms break

    Port_WriteBit(13,0)            '  EN1 GND
    Port_WriteBit(14,1)            '  EN2 +5V

    'Extra informatie van Peter Sprenger
    ' Die Inputs fr WP = PE.5 zbd CD = PB.4 mssen auf low gesetzt werden.
    Port_DataDirBit(37,0)          '  PE.5 = input (output WriteProtect)
    'Port_DataDirBit(12,0)          '  PB.4 = input (ausgang CardDetect)
    Port_WriteBit(37,0)            '
    'Port_WriteBit(12,0)            '

    'Power on -> SD-Card
    Port_WriteBit(14,1)            '  EN2 (PB.6) +5V
    AbsDelay(50)                   '  50ms Pause

    SPI_Enable(80)   '3,6 Mhz
    'SD-Card Fat init
    SDC_Init(fat)

    'Create a new file folders
    SDC_MkDir("0:/CC-PRO/Kees")
    Msg_WriteText ("Passed"): Msg_WriteChar(13)


    'Does the file already exists?
    'If the file is not created
    res=SDC_FOpen(fil, "0:/CC-PRO/Kees/test.txt", FA_READ Or FA_WRITE Or FA_OPEN_EXISTING)
    Msg_WriteInt (res): Msg_WriteChar(13)

    If res=3 Then Msg_WriteText("Geen kaart aanwezig"): End If

    If res=0 Then
        Msg_WriteText ("Res was 0"): Msg_WriteChar(13)
        SDC_FOpen(fil, "0:/CC-PRO/Kees/test.txt", FA_WRITE Or FA_CREATE_ALWAYS)
        End If

    'Read the last position of file and move to end, 20110405
    SDC_FStat( "0:CC-Pro/Kees/Test.txt",SD_Card_fileinfo)
    pos= SD_Card_fileinfo(0)
    Msg_WriteText("Last position on SD-card = "): Msg_WriteInt(pos): Msg_WriteChar(13)
    SDC_FSeek(fil, pos)

    'Writes to a text file
    buf= "Voor de vierde keer aanvulllende info\r\n"
    SDC_FWrite(fil, buf, Str_Len(buf), bytes_written)

    buf= "19520722\r\n"              '\r\n means Return, New line
    SDC_FWrite(fil, buf, Str_Len(buf), bytes_written)
    'Msg_WriteInt(bytes_written)

    buf= "19820807\r\n"
    SDC_FWrite(fil, buf, Str_Len(buf), bytes_written)

    buf= "19850227\r\n"
    SDC_FWrite(fil, buf, Str_Len(buf), bytes_written)

    buf= "SonjaCornelia\r\n"
    SDC_FWrite(fil, buf, Str_Len(buf), bytes_written)

    SDC_FSync(fil)   'Wait until all data are stored
    'File is closed
    SDC_FClose(fil)

'Request for file information, 20110405
SDC_FStat( "0:CC-Pro/Kees/Test.txt",SD_Card_fileinfo)
Msg_WriteInt(SD_Card_fileinfo(0)): Msg_WriteChar(13)
Msg_WriteInt(SD_Card_fileinfo(1)): Msg_WriteChar(13)
Msg_WriteInt(SD_Card_fileinfo(2)): Msg_WriteChar(13)
Msg_WriteInt(SD_Card_fileinfo(3)): Msg_WriteChar(13)

    Msg_WriteText ("Finish")

End Sub
