Statistics
| Branch: | Tag: | Revision:

amiro-os / components / FileSystemInputOutput / FileSystemInputOutputBase.cpp @ 58fe0e0b

History | View | Annotate | Download (6.35 KB)

1
#include <amiro/FileSystemInputOutput/FileSystemInputOutputBase.hpp>
2

    
3
using namespace amiro;
4
using namespace amiro::fileSystemIo;
5

    
6
msg_t
7
FileSystemIoBase::init()
8
{
9
  msg_t returnValue = OK;
10

    
11
  // Get the preamble
12
  const uint8_t bufferSize = sizeof(FileSystemIoBase::preamble_1_1);
13
  uint8_t buffer[bufferSize];
14
  chFileStreamSeek((BaseFileStream*)&at24c01, offsetof(FileSystemIoBase::preamble_1_1, magicByte));
15
  const size_t bytesRead = chSequentialStreamRead((BaseFileStream*)&at24c01, (uint8_t*) buffer, bufferSize);
16

    
17
  // reading failed -> error
18
  if (bytesRead != bufferSize) {
19
    returnValue |= IO_ERROR;
20
    return returnValue;
21
  }
22

    
23
  // magic byte is incorrect -> error
24
  if (buffer[offsetof(FileSystemIoBase::preamble_1_1, magicByte)] != magicByteValue()) {
25
    returnValue |= WRONG_MAGICBYTE_VALUE;
26
    return returnValue;
27
  }
28

    
29
  // BSMV is incorrect -> error
30
  if (buffer[offsetof(FileSystemIoBase::preamble_1_1, byteStructureMajorVersion)] != BSMV) {
31
    returnValue |= WRONG_BSMV;
32
    return returnValue;
33
  }
34

    
35
  // bsmv in smaller -> initialize
36
  if (buffer[offsetof(FileSystemIoBase::preamble_1_1, byteStructureMinorVersion)] < bsmv) {
37
    returnValue |= WRONG_bsmv;
38
    // initialize all missing data
39
    for (uint8_t v = bsmv; v > buffer[offsetof(FileSystemIoBase::preamble_1_1, byteStructureMinorVersion)]; --v)
40
    {
41
      returnValue |= initPreamble(BSMV, v, false);
42
    }
43
  }
44

    
45
  // HMV is incorrect -> error
46
  if (buffer[offsetof(FileSystemIoBase::preamble_1_1, hardwareMajorVersion)] != HMV) {
47
    returnValue |= WRONG_HMV;
48
    return returnValue;
49
  }
50

    
51
  // hmv is incorrect -> error
52
  if (buffer[offsetof(FileSystemIoBase::preamble_1_1, hardwareMinorVersion)] >= hmv) {
53
    returnValue |= WRONG_hmv;
54
  }
55

    
56
  return returnValue;
57
}
58

    
59
msg_t
60
FileSystemIoBase::
61
getCheck() {
62

    
63
  // Get the preamble
64
  const uint8_t bufferSize = sizeof(FileSystemIoBase::preamble_1_1);
65
  uint8_t buffer[bufferSize];
66
  chFileStreamSeek((BaseFileStream*)&at24c01, offsetof(FileSystemIoBase::preamble_1_1, magicByte));
67
  const size_t bytesRead = chSequentialStreamRead((BaseFileStream*)&at24c01, (uint8_t*) buffer, bufferSize);
68

    
69
  if (bytesRead != bufferSize) {
70
    return IO_ERROR;
71
  } else if (buffer[offsetof(FileSystemIoBase::preamble_1_1, magicByte)] != magicByteValue()) {
72
    return WRONG_MAGICBYTE_VALUE;
73
  } else if (buffer[offsetof(FileSystemIoBase::preamble_1_1, byteStructureMajorVersion)] != BSMV) {
74
    return WRONG_BSMV;
75
  } else if (buffer[offsetof(FileSystemIoBase::preamble_1_1, byteStructureMinorVersion)] < bsmv) {
76
    return WRONG_bsmv;
77
  } else if (buffer[offsetof(FileSystemIoBase::preamble_1_1, hardwareMajorVersion)] != HMV) {
78
    return WRONG_HMV;
79
  } else if (buffer[offsetof(FileSystemIoBase::preamble_1_1, hardwareMinorVersion)] < hmv) {
80
    return WRONG_hmv;
81
  } else {
82
    return OK;
83
  }
84
}
85

    
86
msg_t
87
FileSystemIoBase::initPreamble(const uint8_t BSMV, const uint8_t bsmv, const bool strict)
88
{
89
  uint8_t buffer[3] = {};
90

    
91
  if (strict) {
92
    // initialize the magic byte
93
    chFileStreamSeek((BaseFileStream*)&this->at24c01, offsetof(FileSystemIoBase::preamble_1_1, magicByte));
94
    buffer[0] = magicByteValue();
95
    if (chSequentialStreamWrite((BaseFileStream*)&this->at24c01, buffer, 1) != 1) {
96
      return IO_ERROR;
97
    }
98
  }
99

    
100
  switch (BSMV)
101
  {
102
    case 1:
103
      if (strict) {
104
        // initialize the BSMV
105
        chFileStreamSeek((BaseFileStream*)&this->at24c01, offsetof(FileSystemIoBase::preamble_1_1, byteStructureMajorVersion));
106
        buffer[0] = BSMV;
107
        if (chSequentialStreamWrite((BaseFileStream*)&this->at24c01, buffer, 1) != 1) {
108
          return IO_ERROR;
109
        }
110
      }
111
      // initialize the bsmv
112
      chFileStreamSeek((BaseFileStream*)&this->at24c01, offsetof(FileSystemIoBase::preamble_1_1, byteStructureMinorVersion));
113
      buffer[0] = bsmv;
114
      if (chSequentialStreamWrite((BaseFileStream*)&this->at24c01, buffer, 1) != 1) {
115
        return IO_ERROR;
116
      }
117
      // initialize the content
118
      switch (bsmv)
119
      {
120
        case 2:
121
          // initialize the board ID
122
          chFileStreamSeek((BaseFileStream*)&this->at24c01, offsetof(FileSystemIoBase::preamble_1_2, boardId));
123
          buffer[0] = 0;
124
          if (chSequentialStreamWrite((BaseFileStream*)&this->at24c01, buffer, 1) != 1) {
125
            return IO_ERROR;
126
          }
127
          if (!strict) {
128
            break;
129
          }
130

    
131
        case 1:
132
          // initialize the bsmv, the HMV and the hmv
133
          chFileStreamSeek((BaseFileStream*)&this->at24c01, offsetof(FileSystemIoBase::preamble_1_1, byteStructureMinorVersion));
134
          buffer[0] = bsmv;
135
          buffer[1] = this->HMV;
136
          buffer[2] = this->hmv;
137
          if (chSequentialStreamWrite((BaseFileStream*)&this->at24c01, buffer, 3) != 3) {
138
            return IO_ERROR;
139
          }
140
          if (!strict) {
141
            break;
142
          }
143

    
144
        default:
145
          break;
146

    
147
      }
148
      break;
149

    
150
    default:
151
      break;
152
  }
153

    
154
  return OK;
155
}
156

    
157
msg_t
158
FileSystemIoBase::
159
resetMemory() {
160
  return initPreamble(this->BSMV, this->bsmv, true);
161
}
162

    
163
msg_t
164
FileSystemIoBase::
165
getBoardId (uint8_t *buffer) {
166

    
167
  msg_t bytesRead = 0;
168
  // Work around, because stm32f1 cannot read a single byte
169
  const uint8_t bufferSize = 2;
170
  uint8_t bufferTmp[bufferSize] = {};
171

    
172
  // Get the data out of the memory
173
  switch (this->BSMV) {
174
    case 0x01u:
175
      // Set the pointer to the address
176
      chFileStreamSeek((BaseFileStream*)&at24c01, offsetof(FileSystemIoBase::preamble_1_2, boardId));
177
      if (this->bsmv >= 0x02u) {
178
        bytesRead = chSequentialStreamRead((BaseFileStream*)&at24c01, (uint8_t*) bufferTmp, bufferSize);
179
      } else {
180
        return NOT_IMPLEMENTED;
181
      }
182
      break;
183
    }
184

    
185
  // Store only the board id
186
  *buffer = bufferTmp[0];
187

    
188
    if (bytesRead != bufferSize) {
189
      return IO_ERROR;
190
    } else {
191
      return OK;
192
    }
193
}
194

    
195
msg_t
196
FileSystemIoBase::
197
setBoardId (uint8_t buffer) {
198

    
199
  msg_t bytesWritten = 0;
200
  const uint8_t bufferSize = sizeof(uint8_t);
201

    
202
  // Get the data out of the memory
203
  switch (this->BSMV) {
204
    case 0x01u:
205
      // Set the pointer to the address
206
      chFileStreamSeek((BaseFileStream*)&at24c01, offsetof(FileSystemIoBase::preamble_1_2, boardId));
207
      if (this->bsmv >= 0x02u) {
208
        bytesWritten = chSequentialStreamWrite((BaseFileStream*)&at24c01, (const uint8_t*) &buffer, bufferSize);
209
      } else {
210
        return NOT_IMPLEMENTED;
211
      }
212
      break;
213
    }
214

    
215
    if (bytesWritten != bufferSize) {
216
      return IO_ERROR;
217
    } else {
218
      return OK;
219
    }
220
}