Forum: Mikrocontroller und Digitale Elektronik VDrive probleme beim Datenspeichern über SPI


von Martin R. (rama123)


Lesenswert?

Hallo,

ich habe ein Probelm mit dem VDrive2 Modul von Vinculum. Ich nutze einen 
PIC18F4553, mit dem ich Daten über den AD-Wandler (100Hz) messe und 
diese dann per SPI auf einen Stick im VDrive speichern möchte. Ich 
verwende den VDrive in Short Command Mode.

Mein Problem ist jetzt, dass wenn ich zu viele Daten speichern möchte, 
also die Messung zu lange laufen lasse blinkt das VDrive zwar, erstellt 
aber keine Datei. Wenn ich kürzer messe bekomme ich zwar eine Datei aber 
es fehlen Daten. Irgendwie fehlen zwischendurch genau 16 Byte.

Hier ist mein Code:

Soft SPI:
1
/*****************************************************************************/
2
/* Beschreibung:
3
/*   Funktion zum Schreiben in das VDrive2. Der Vorgang wird nur ausgeführt,
4
/*   wenn Daten im Ausgangspuffer vorhanden sind.
5
/*****************************************************************************/
6
void WriteVDrive() {          //void WriteVDrive(byte Value) {
7
  byte CheckTransmission = 1;
8
  byte Value;
9
10
  if (Flags.VDriveBufferData == 1) 
11
  {
12
    Value = VDriveBuffer[VDriveBufferPointerRead];
13
  
14
    // Sequenz einleiten
15
    SPI_SOFT_CLEAR_SDO();
16
    SPI_SOFT_CLEAR_SCLK();
17
    SPI_CS_VDrive();
18
    SPI_SOFT_SET_SDO();
19
    SPI_SOFT_CLOCK();
20
    SPI_SOFT_CLEAR_SDO();
21
    SPI_SOFT_CLOCK();
22
    
23
    // Daten schreiben
24
    SPI_SOFT_CLOCK();
25
    if (Value & 0x80) {
26
      SPI_SOFT_SET_SDO();
27
    } else {
28
      SPI_SOFT_CLEAR_SDO();
29
    }
30
    SPI_SOFT_CLOCK();
31
    Value <<= 1;
32
    if (Value & 0x80) {
33
      SPI_SOFT_SET_SDO();
34
    } else {
35
      SPI_SOFT_CLEAR_SDO();
36
    }
37
    SPI_SOFT_CLOCK();
38
    Value <<= 1;
39
    if (Value & 0x80) {
40
      SPI_SOFT_SET_SDO();
41
    } else {
42
      SPI_SOFT_CLEAR_SDO();
43
    }
44
    SPI_SOFT_CLOCK();
45
    Value <<= 1;
46
    if (Value & 0x80) {
47
      SPI_SOFT_SET_SDO();
48
    } else {
49
      SPI_SOFT_CLEAR_SDO();
50
    }
51
    SPI_SOFT_CLOCK();
52
    Value <<= 1;
53
    if (Value & 0x80) {
54
      SPI_SOFT_SET_SDO();
55
    } else {
56
      SPI_SOFT_CLEAR_SDO();
57
    }
58
    SPI_SOFT_CLOCK();
59
    Value <<= 1;
60
    if (Value & 0x80) {
61
      SPI_SOFT_SET_SDO();
62
    } else {
63
      SPI_SOFT_CLEAR_SDO();
64
    }
65
    SPI_SOFT_CLOCK();
66
    Value <<= 1;
67
    if (Value & 0x80) {
68
      SPI_SOFT_SET_SDO();
69
    } else {
70
      SPI_SOFT_CLEAR_SDO();
71
    }
72
    SPI_SOFT_CLOCK();
73
    Value <<= 1;
74
    if (Value & 0x80) {
75
      SPI_SOFT_SET_SDO();
76
    } else {
77
      SPI_SOFT_CLEAR_SDO();
78
    }
79
  
80
    SPI_SOFT_CLOCK();
81
    CheckTransmission = SPI_SOFT_SDI;
82
  
83
    // Taktzyklus warten
84
    SPI_SOFT_CLOCK();
85
    SPI_CS_None();
86
    SPI_SOFT_CLOCK();
87
    SPI_SOFT_CLOCK();
88
89
    if (CheckTransmission == 0)
90
    {
91
      // Daten erfolgreich geschrieben
92
93
      VDriveBufferPointerRead++;
94
      if (VDriveBufferPointerRead == VDRIVE_BUFFER_LENGTH) VDriveBufferPointerRead = 0;
95
      if (VDriveBufferPointerRead == VDriveBufferPointerWrite) Flags.VDriveBufferData = 0;
96
    }
97
  }
98
}

Funktion "AddVDriveBuffer"
1
/*****************************************************************************/
2
/* Beschreibung:
3
/*   Funktion zum Hinzufügen eines Wertes zum Ausgangspuffer für das VDrive.
4
/*
5
/* Parameter:
6
/*   Value: Wert
7
/*****************************************************************************/
8
void AddVDriveBuffer(byte Value) {
9
  VDriveBuffer[VDriveBufferPointerWrite] = Value;
10
  VDriveBufferPointerWrite++;
11
  if (VDriveBufferPointerWrite == VDRIVE_BUFFER_LENGTH) VDriveBufferPointerWrite = 0;
12
  Flags.VDriveBufferData = 1;
13
}

Hauptprogramm in dem ich messe
1
void ProcessIO(void)
2
{
3
int I;
4
dword TempDWord;
5
6
  // VDrive auslesen
7
  VDriveData vdrive;
8
    
9
      vdrive.Status = 0;
10
      while (vdrive.Status == 0) {
11
        vdrive = ReadVDrive();
12
      }
13
14
  // Vorhandene Daten ins VDrive schreiben
15
INTCONbits.GIE = 0;      // Interrupt off
16
  for (I=1; I<=16; I++)
17
  { 
18
    WriteVDrive();
19
  }
20
INTCONbits.GIE = 1;      // Interrupt on
21
22
  if((SWITCH_Start_Pressed())&&(Flags.CheckMeasurementDone == 0))
23
  {
24
    if (Flags.MeasurementActive  ==  0)
25
    {
26
      LED_Measurement_On();
27
28
      //Start der Messung aktivieren
29
      Flags.MeasurementActive = 1;
30
      Ticks = 0;
31
      T0CONbits.TMR0ON = 1;
32
      Flags.WriteUSB = 1;
33
      Flags.WriteDataStart = 1;
34
  
35
      // VDrive Kommandomodus auf "Short Command Set" setzen
36
      AddVDriveBuffer(0x10);
37
      AddVDriveBuffer(0x0D);
38
      Flags.VDriveBufferData = 1;
39
  
40
      // Befehle zum öffnen einer Datei auf dem Stick ins SRAM speichern
41
      TempDWord = GetVDriveDateFromClock();
42
      AddVDriveBuffer(0x09); AddVDriveBuffer(0x20); AddVDriveBuffer(0x4D); AddVDriveBuffer(0x45);
43
      AddVDriveBuffer(0x41); AddVDriveBuffer(0x53); AddVDriveBuffer(0x55); AddVDriveBuffer(0x52);
44
      AddVDriveBuffer(0x45); AddVDriveBuffer(0x2E); AddVDriveBuffer(0x4E); AddVDriveBuffer(0x50);
45
      AddVDriveBuffer(0x47); AddVDriveBuffer(0x0D);
46
    }
47
    // Messung durchführen
48
    if (Flags.MeasurementActive && Flags.MeasurementGetData) 
49
    {
50
      Flags.MeasurementGetData = 0;
51
      if (Ticks <= 200) 
52
      {
53
        // Analogspannungen einlesen
54
        ANALOG_CS_Spannung();
55
        while (!ANALOG_MEASUREMENT_DONE) {}
56
        Spannung = ADRESH;
57
        ANALOG_CS_Strom();
58
        while (!ANALOG_MEASUREMENT_DONE) {}
59
        Strom = ADRESH;
60
61
        AddVDriveBuffer(0x08); AddVDriveBuffer(0x20); AddVDriveBuffer(0x00); AddVDriveBuffer(0x00);
62
        AddVDriveBuffer(0x00); AddVDriveBuffer(0x01); AddVDriveBuffer(0x0d);
63
        AddVDriveBuffer(Ticks);
64
      
65
      if (Ticks >200)
66
      {
67
        // File auf dem VDrive schließen
68
        AddVDriveBuffer(0x0A); AddVDriveBuffer(0x20); AddVDriveBuffer(0x4D); AddVDriveBuffer(0x45);
69
        AddVDriveBuffer(0x41); AddVDriveBuffer(0x53); AddVDriveBuffer(0x55); AddVDriveBuffer(0x52);
70
        AddVDriveBuffer(0x45); AddVDriveBuffer(0x2E); AddVDriveBuffer(0x4E); AddVDriveBuffer(0x50);
71
        AddVDriveBuffer(0x47); AddVDriveBuffer(0x0D);
72
73
        // Messung beendet
74
        LED_Measurement_Off();
75
        Flags.MeasurementActive = 0; 
76
        T0CONbits.TMR0ON = 0;
77
        Flags.CheckMeasurementDone = 1;    //Messung beendet
78
        Flags.ButtonPress = 0;        
79
      }
80
81
    }
82
  }
83
  if (!SWITCH_Start_Pressed())
84
  {  
85
    LED_Measurement_Off();
86
87
    // Messung beendet
88
    Flags.MeasurementActive = 0; 
89
    T0CONbits.TMR0ON = 0;
90
    Flags.CheckMeasurementDone = 0;    // Neue Messung kann gestartet werden
91
    Flags.ButtonPress = 0;        
92
  }
93
94
}

Ich hoffe es kann mir jemand helfen.
Gruß Martin

von Matthias K. (matthiask)


Lesenswert?

Hier findest Du SPI-Routinen für den Vinculum USB Host:
http://www.mikrocontroller.net/articles/USB-Stick_am_Mikrocontroller

Ich vermute, es gibt einen Bufferüberlauf beim schnellen senden. Wenn 
ich es richtig überflogen habe, wertest Du nur das Statusbit aus, ob ein 
Byte akzepiert wurde oder nicht. Das reicht nicht aus, der Buffer kann 
trotzdem überlaufen.

Werte also noch zusätzlich das Status-BYTE aus. Darin ist ein Bit 
RECEIVE-BUFFER voll (RXF#). Details in den Projektarchiv im Artikel 
oben.

Bitte melde dich an um einen Beitrag zu schreiben. Anmeldung ist kostenlos und dauert nur eine Minute.
Bestehender Account
Schon ein Account bei Google/GoogleMail? Keine Anmeldung erforderlich!
Mit Google-Account einloggen
Noch kein Account? Hier anmelden.