Hanna
Sourcecode Batteriemanagementsystem
sdcard_driver.c
gehe zur Dokumentation dieser Datei
1 
28 #include "sdcard_driver.h"
29 
30 #include <avr/pgmspace.h>
31 #include <stdio.h>
32 #include <string.h>
33 #include <stdint.h>
34 
35 //#################################### SD Card
36 #include "sdcard/fat.h"
37 #include "sdcard/fat_config.h"
38 #include "sdcard/partition.h"
39 #include "sdcard/sd_raw.h"
40 #include "sdcard/sd_raw_config.h"
41 
42 #define DEBUG 0
43 
44 struct partition_struct* partition;
45 struct fat_fs_struct* fs;
46 struct fat_dir_struct* dd;
47 struct fat_dir_entry_struct directory;
48 struct fat_file_struct* fd;
49 struct fat_dir_entry_struct dir_entry;
50 struct fat_dir_entry_struct file_entry;
51 
52 uint8_t file_open = 0, card_open = 0;
53 
54 //##########################################################################################
55 uint16_t file_getAnzLine ( void ) // Anzahl Zeilen aktuelles file
56 {
57  uint16_t counterLines = 0;
58  int32_t read_offset = 0;
59 
60  fat_seek_file ( fd, &read_offset, FAT_SEEK_SET );
61  if ( fd )
62  {
63  uint8_t buffer[30];
64  intptr_t count;
65  while ( (count = fat_read_file ( fd, buffer, sizeof(buffer) )) > 0 )
66  {
67  for ( intptr_t i = 0; i < count; ++i )
68  {
69  if ( buffer[i] == '\r' )
70  {
71  counterLines++;
72  }
73  }
74  }
75  }
76  return counterLines + 1;
77 }
78 
79 void set_filepointer_position(int32_t offset){
80  fat_seek_file ( fd, &offset, FAT_SEEK_SET );
81 }
82 
83 //##########################################################################################
84 uint8_t file_getLine ( uint16_t lineNr, char* zielbuffer )
85 {
86  if ( fd )
87  {
88  if ( lineNr < 1 )
89  {
90  return 0;
91  }
92  uint8_t bufferlen = 100; // maximale Zeilenlänge von 100 Zeichen
93  uint8_t arbeitsbuffer[bufferlen];
94  uint16_t counterLines = 0;
95  int32_t pos_after_last_enter = 0;
96  uint8_t count;
97 
98 // int32_t write_offset = 0;
99 //
100 // fat_seek_file ( fd, &write_offset, FAT_SEEK_SET );
101 
102  //########## Anfang der n-ten Zeile suchen und position in "pos_after_last_enter" zurückgeben
103  //########## es werden alle '\r' gezählt bis die Anfangsposition der gesuchten Zeile gefunden ist
104 
105  while ( counterLines < lineNr - 1 )
106  {
107  if ( !fat_seek_file ( fd, &pos_after_last_enter, FAT_SEEK_SET ) )
108  {/* error*/
109  }
110 
111  count = fat_read_file ( fd, arbeitsbuffer, bufferlen );
112  for ( intptr_t i = 0; i < count; ++i )
113  {
114  if ( arbeitsbuffer[i] == '\r' )
115  {
116  counterLines++;
117  pos_after_last_enter += (i + 1); // Zeiger auf Zeichen nach dem letzten Enter
118  break;
119  }
120  }
121  }
122 
123  //########## Daten auslesen und in Zielbuffer schreiben
124  if ( !fat_seek_file ( fd, &pos_after_last_enter, FAT_SEEK_SET ) )
125  {/* error*/
126  }
127  count = fat_read_file ( fd, arbeitsbuffer, bufferlen );
128 
129  for ( intptr_t i = 0; i < count; ++i )
130  {
131  zielbuffer[i] = arbeitsbuffer[i];
132  if ( arbeitsbuffer[i] == '\r' )
133  {
134  zielbuffer[i + 1] = 0;
135  return i; // return Zeilenlänge
136  }
137  }
138  }
139  return 0;
140 }
141 //##########################################################################################
142 uint32_t strtolong ( const char* str )
143 {
144  uint32_t l = 0;
145  while ( *str >= '0' && *str <= '9' )
146  l = l * 10 + (*str++ - '0');
147 
148  return l;
149 }
150 //##########################################################################################
151 uint8_t find_file_in_dir ( struct fat_fs_struct* fs, struct fat_dir_struct* dd, const char* name, struct fat_dir_entry_struct* dir_entry )
152 {
153  while ( fat_read_dir ( dd, dir_entry ) )
154  {
155  if ( strcmp ( dir_entry->long_name, name ) == 0 )
156  {
157  fat_reset_dir ( dd );
158  return 1;
159  }
160  }
161 
162  return 0;
163 }
164 //##########################################################################################
165 struct fat_file_struct* open_file_in_dir ( struct fat_fs_struct* fs, struct fat_dir_struct* dd, const char* name )
166 {
167  struct fat_dir_entry_struct file_entry;
168  if ( !find_file_in_dir ( fs, dd, name, &file_entry ) ) return 0;
169 
170  return fat_open_file ( fs, &file_entry );
171 }
172 //##########################################################################################
173 uint8_t print_disk_info ( const struct fat_fs_struct* fs )
174 {
175 
176  if ( !fs ) return 0;
177  struct sd_raw_info disk_info;
178  if ( !sd_raw_get_info ( &disk_info ) ) return 0;
179  printf ( "#........\r\n" );
180  printf ( "#manuf : %X\r\n", disk_info.manufacturer );
181  printf ( "#oem : %s\r\n", (char*) disk_info.oem );
182  printf ( "#prod : %s\r\n", (char*) disk_info.product );
183  printf ( "#rev : %X\r\n", disk_info.revision );
184  printf ( "#serial: %ld\r\n", disk_info.serial );
185  printf ( "#date : %i / %i\r\n", disk_info.manufacturing_month, disk_info.manufacturing_year );
186  printf ( "#size : %ld MB\r\n", ((long) disk_info.capacity / 1024 / 1024) );
187  printf ( "#copy : %i\r\n", disk_info.flag_copy );
188  printf ( "#wr.pr.: %i / %i\r\n", disk_info.flag_write_protect_temp, disk_info.flag_write_protect );
189  printf ( "#format: %i\r\n", disk_info.format );
190  printf ( "#free : %ld / %ld\r\n\n", (long) fat_get_fs_free ( fs ), (long) fat_get_fs_size ( fs ) );
191  printf ( "#........\r\n" );
192 
193  return 1;
194 }
195 
196 //##########################################################################################
197 void sd_get_Directory ( void )
198 {
199  printf ( "#........Directory:\r\n" );
200  while ( fat_read_dir ( dd, &dir_entry ) )
201  {
202  uint8_t spaces = 20 - strlen ( dir_entry.long_name );
203 
204  printf ( "# %s", (char*) dir_entry.long_name );
205 
206  for ( uint8_t i = 0; i < spaces; i++ )
207  {
208  printf ( " " );
209  }
210 
211  printf ( "%ld\r\n", dir_entry.file_size );
212  }
213 }
214 //##########################################################################################
215 void sd_card_open ( void ) //Logging File auf SD-Karte oeffnen und auf das Ende positionieren.
216 {
217  if ( card_open == 1 ) return;
218 
219  //############# setup sd card slot
220  if ( !sd_raw_init () )
221  {
222 #if DEBUG
223  printf ( "#MMC/SD initialisation failed\r\n" );
224 #endif
225  }
226 
227  else
228  {
229  card_open = 1;
230 //############# open first partition
232 
233  // If the partition did not open, assume the storage device is a "superfloppy", i.e. has no MBR.
234  if ( !partition )
235  {
237  if ( !partition )
238  {
239 #if DEBUG
240  printf("#opening partition failed\r\n");
241 #endif
242  }
243  }
244 
245  //############# open file system
246  fs = fat_open ( partition );
247  if ( !fs )
248  {
249 #if DEBUG
250  printf("#opening filesystem failed\r\n");
251 #endif
252  }
253 
254  //############# open root directory
255  fat_get_dir_entry_of_path ( fs, "/", &directory );
256 
257  dd = fat_open_dir ( fs, &directory );
258  if ( !dd )
259  {
260 #if DEBUG
261  printf("#opening root directory failed\r\n");
262 #endif
263  }
264 
265 #if DEBUG
266  print_disk_info(fs); //print some card information as a boot message
267  sd_get_Directory();//print directory entry
268 #endif
269 
270 //############
271 #if DEBUG
272  printf ( "#Card open\r\n" );
273 #endif
274  }
275 }
276 //##################################################################################################
277 void sd_card_close ( void )
278 {
279  if ( card_open == 1 && card_open == 0 ) // Zur Sicherheit noch mal einen sync, damit ist das FS konsistent
280  {
281  fat_close_dir ( dd ); // close directory
282 #if DEBUG
283  printf ( "#close: directory\r\n" );
284 #endif
285  fat_close ( fs ); // close file system
286 #if DEBUG
287  printf ( "#close: file system \r\n" );
288 #endif
289  partition_close ( partition ); // close partition
290 #if DEBUG
291  printf ( "#close: partition\r\n" );
292 #endif
293  card_open = 0;
294  }
295 }
296 //##################################################################################################
297 int sd_file_open ( const char* filename )
298 {
299  if ( file_open == 1 )
300  {
301 #if DEBUG
302  printf ( "#File schon offen\r\n" );
303 #endif
304  return 1;
305  }
306 
307  fd = open_file_in_dir ( fs, dd, filename );
308  if ( !fd )
309  {
310 #if DEBUG
311  printf ( "#Fehler beim oeffen von: %s", filename );
312 #endif
313  file_open = 0;
314  }
315  else
316  {
317  file_open = 1;
318 #if DEBUG
319  printf ( "#File: %s geoeffnet\r\n", filename );
320 #endif
321  }
322  return file_open;
323 }
324 //##########################################################################################
325 uint8_t sd_file_delete ( const char* filename )
326 {
327  struct fat_dir_entry_struct file_entry;
328 
329  if ( !find_file_in_dir ( fs, dd, filename, &file_entry ) ) return 0;
330 
331  return fat_delete_file ( fs, &file_entry );
332 }
333 //##################################################################################################
334 void sd_file_new ( const char* filename )
335 {
336  // Nun ein neues File erstellen
337  if ( !fat_create_file ( dd, filename, &file_entry ) )
338  {
339 #if DEBUG
340  printf ( "#Fehler beim anlegen von: %s\r\n", filename );
341 #endif
342  }
343  else
344  {
345 #if DEBUG
346  printf ( "#File: %s erfolgreich angelegt\r\n", filename );
347 #endif
348  sd_raw_sync ();
349  }
350 }
351 //##################################################################################################
352 void sd_file_close ( void )
353 {
354  if ( file_open == 1 ) // Zur Sicherheit noch mal einen sync, damit ist das FS konsistent
355  {
356  sd_raw_sync ();
357  fat_close_file ( fd );
358 #if DEBUG
359  printf ( "#close: file\r\n" );
360 #endif
361  file_open = 0;
362  }
363 }
364 //##########################################################################################
365 void sd_file_write_append ( const char* line )
366 {
367  if ( file_open == 1 )
368  {
369  // Auf das Ende des Files positionieren
370  // FAT_SEEK_END und offset=0 heißen, vom Ende der Datei aus 0 Bytes lesen,
371  // mit anderen Worten, an das Ende des Files gehen.
372  int32_t write_offset = 0;
373  fat_seek_file ( fd, &write_offset, FAT_SEEK_END );
374 
375  uint8_t data_len = strlen ( line );
376  if ( fat_write_file ( fd, (uint8_t*) line, data_len ) != data_len )
377  {
378 #if DEBUG
379  printf ( "#error writing to file\r\n" );
380 #endif
381  return;
382  }
383  sd_raw_sync ();
384  }
385 }
386 
387 //##########################################################################################
388 void file_write_line ( uint16_t lineNr, const char* line )
389 {
390  if ( file_open == 1 )
391  {
392  if ( lineNr < 1 )
393  {
394  return;
395  }
396  uint8_t bufferlen = 100; // maximale Zeilenlänge von 100 Zeichen
397  uint8_t arbeitsbuffer[bufferlen];
398  uint16_t counterLines = 0;
399  int32_t pos_after_last_enter = 0;
400  uint8_t count;
401 
402  //########## Anfang der n-ten Zeile suchen und position in "pos_after_last_enter" zurückgeben
403  //########## es werden alle '\r' gezählt bis die Anfangsposition der gesuchten Zeile gefunden ist
404 
405  while ( counterLines < lineNr - 1 )
406  {
407  if ( !fat_seek_file ( fd, &pos_after_last_enter, FAT_SEEK_SET ) )
408  {/* error*/
409  }
410 
411  count = fat_read_file ( fd, arbeitsbuffer, bufferlen );
412  for ( intptr_t i = 0; i < count; ++i )
413  {
414  if ( arbeitsbuffer[i] == '\r' )
415  {
416  counterLines++;
417  pos_after_last_enter += (i + 1); // Zeiger auf Zeichen nach dem letzten Enter
418  break;
419  }
420  }
421  }
422 
423  fat_seek_file ( fd, &pos_after_last_enter, FAT_SEEK_SET );
424 
425  uint8_t data_len = strlen ( line );
426  if ( fat_write_file ( fd, (uint8_t*) line, data_len ) != data_len )
427  {
428 #if DEBUG
429  printf ( "#error writing to file\r\n" );
430 #endif
431  return;
432  }
433  sd_raw_sync ();
434  }
435 }
436 
437 uint8_t sd_get_disk_info ( void )
438 {
439  return print_disk_info ( fs );
440 }
441 
uint8_t sd_raw_read_interval(offset_t offset, uint8_t *buffer, uintptr_t interval, uintptr_t length, sd_raw_read_interval_handler_t callback, void *p)
Definition: sd_raw.c:563
struct fat_dir_struct * fat_open_dir(struct fat_fs_struct *fs, const struct fat_dir_entry_struct *dir_entry)
Definition: fat.c:1324
uint8_t fat_reset_dir(struct fat_dir_struct *dd)
Definition: fat.c:1487
void fat_close_dir(struct fat_dir_struct *dd)
Definition: fat.c:1363
uint8_t sd_raw_get_info(struct sd_raw_info *info)
Definition: sd_raw.c:848
offset_t fat_get_fs_size(const struct fat_fs_struct *fs)
Definition: fat.c:2326
uint8_t sd_raw_init()
Definition: sd_raw.c:171
uint8_t fat_read_dir(struct fat_dir_struct *dd, struct fat_dir_entry_struct *dir_entry)
Definition: fat.c:1382
SD-Card Reader Bibliothek von Roland Riegel.
intptr_t fat_read_file(struct fat_file_struct *fd, uint8_t *buffer, uintptr_t buffer_len)
Definition: fat.c:942
SD-Card Reader Bibliothek von Roland Riegel.
uint8_t fat_delete_file(struct fat_fs_struct *fs, struct fat_dir_entry_struct *dir_entry)
Definition: fat.c:2018
SD-Card Reader Bibliothek von Roland Riegel.
uint8_t fat_create_file(struct fat_dir_struct *parent, const char *file, struct fat_dir_entry_struct *dir_entry)
Definition: fat.c:1971
uint32_t file_size
Definition: fat.h:99
SD-Card Reader Bibliothek von Roland Riegel. MMC/SD/SDHC raw access header (license: GPLv2 or LGPLv2...
Datei aus der SD-Card Reader Bibliothek von M. Junghans.
uint8_t partition_close(struct partition_struct *partition)
Definition: partition.c:149
#define FAT_SEEK_SET
Definition: fat.h:65
uint8_t sd_raw_read(offset_t offset, uint8_t *buffer, uintptr_t length)
Definition: sd_raw.c:453
uint8_t sd_raw_sync()
Definition: sd_raw.c:822
void fat_close_file(struct fat_file_struct *fd)
Definition: fat.c:913
struct partition_struct * partition_open(device_read_t device_read, device_read_interval_t device_read_interval, device_write_t device_write, device_write_interval_t device_write_interval, int8_t index)
Definition: partition.c:77
#define FAT_SEEK_END
Definition: fat.h:69
void fat_close(struct fat_fs_struct *fs)
Definition: fat.c:302
uint8_t fat_get_dir_entry_of_path(struct fat_fs_struct *fs, const char *path, struct fat_dir_entry_struct *dir_entry)
Definition: fat.c:808
intptr_t fat_write_file(struct fat_file_struct *fd, const uint8_t *buffer, uintptr_t buffer_len)
Definition: fat.c:1032
struct fat_file_struct * fat_open_file(struct fat_fs_struct *fs, const struct fat_dir_entry_struct *dir_entry)
Definition: fat.c:878
uint8_t fat_seek_file(struct fat_file_struct *fd, int32_t *offset, uint8_t whence)
Definition: fat.c:1186
struct fat_fs_struct * fat_open(struct partition_struct *partition)
Definition: fat.c:250
uint8_t sd_raw_write_interval(offset_t offset, uint8_t *buffer, uintptr_t length, sd_raw_write_interval_handler_t callback, void *p)
Definition: sd_raw.c:781
char long_name[32]
Definition: fat.h:87
uint8_t sd_raw_write(offset_t offset, const uint8_t *buffer, uintptr_t length)
Definition: sd_raw.c:673
SD-Card Reader Bibliothek von Roland Riegel.
offset_t fat_get_fs_free(const struct fat_fs_struct *fs)
Definition: fat.c:2348