bexus_sd.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #include "bexus_sd.h"
  2. /**----------------variables---------------------------*/
  3. SD_HandleTypeDef hsd1;
  4. HAL_SD_CardInfoTypeDef SDCardInfo1;
  5. FRESULT status_SD;
  6. FATFS SDFatFs;
  7. FIL File_SD;
  8. FILINFO fno;
  9. char * buff_write_SD;
  10. char * buff_read_SD;
  11. char buff_filename_SD[15]="log0000.log";
  12. uint32_t byteswritten_SD, bytesread_SD;
  13. uint32_t File_Index=0;
  14. uint32_t errorCount = 0;
  15. uint32_t Dateigrosse = 0;
  16. uint8_t sd_errortype=0;
  17. uint32_t sdTime, tmp;
  18. char eol[2] = "\r\n";
  19. /**-----------------------------------------------------*/
  20. /* Work-memory needed for compression. Allocate memory in units
  21. * of 'lzo_align_t' (instead of 'char') to make sure it is properly aligned.
  22. */
  23. /*
  24. #define HEAP_ALLOC(var,size) lzo_align_t __LZO_MMODEL var [ ((size) + (sizeof(lzo_align_t) - 1)) / sizeof(lzo_align_t) ]
  25. static HEAP_ALLOC(wrkmem, LZO1X_1_MEM_COMPRESS); // standard ist 16384 bytes als LZO1X_1_MEM_COMPRESS
  26. int r;
  27. */
  28. /**------------------functions--------------------------*/
  29. void SD_FileName(){
  30. File_Index = 0;
  31. sprintf(buff_filename_SD,"log%04d.log", File_Index);
  32. f_close(&File_SD);
  33. while(FR_OK == f_stat(buff_filename_SD, &fno)) { //Wenn eine Datei mit bestehende Namen gefunden wurde, index inkrementieren
  34. File_Index++;
  35. sprintf(buff_filename_SD,"log%04d.log", File_Index);
  36. }
  37. status_SD = f_open(&File_SD,buff_filename_SD,FA_WRITE|FA_CREATE_ALWAYS);
  38. if(FR_OK != status_SD)
  39. Error_SD(10);
  40. }
  41. void Error_SD(int Error_Type_SD){
  42. if(Error_Type_SD != 99 )
  43. errorCount++;
  44. sd_errortype = Error_Type_SD;
  45. if(Error_Type_SD == 9 && errorCount>10){
  46. SD_FileName();
  47. }
  48. if(Error_Type_SD == 3 && errorCount % 100 == 1) {
  49. SD_FileName();
  50. }
  51. }
  52. void SD_Init(){
  53. status_SD = f_mount(&SDFatFs,"",1); //SD slot 1
  54. if(FR_OK != status_SD) {
  55. HAL_GPIO_WritePin(GPIOG,GPIO_PIN_12,GPIO_PIN_SET);
  56. HAL_GPIO_WritePin(GPIOG,GPIO_PIN_14,GPIO_PIN_RESET);
  57. HAL_Delay(1);
  58. status_SD = f_mount(&SDFatFs,"",1); //SD slot 2
  59. }
  60. if(FR_OK != status_SD) {
  61. Error_SD(0);
  62. }
  63. SD_FileName();
  64. }
  65. void SD_Write(){
  66. tmp = millis();
  67. //for(int i=0; i<DATASET_LENGTH; i++) {
  68. //status_SD = f_write(&File_SD, &Dataset_Master[buff_sensor_data_index].pressure_cis_raw, 20 * 4, &byteswritten_SD);
  69. //status_SD |= f_write(&File_SD, &Dataset_Master[buff_sensor_data_index].longitude, 8 * 4, &byteswritten_SD);
  70. //status_SD = f_write(&File_SD, &Dataset_Slave[buff_sensor_data_index].pressure_cis_raw, 20 * 4, &byteswritten_SD);
  71. //status_SD |= f_write(&File_SD, &Dataset_Slave[buff_sensor_data_index].longitude, 8 * 4, &byteswritten_SD);
  72. status_SD = f_write(&File_SD, &Dataset_Master[buff_sensor_data_index], sizeof(Dataset_Master[buff_sensor_data_index]), &byteswritten_SD);
  73. status_SD |= f_write(&File_SD, &Dataset_Slave[buff_sensor_data_index], sizeof(Dataset_Slave[buff_sensor_data_index]), &byteswritten_SD);
  74. status_SD |= f_write(&File_SD, &Dataset_Fusion[buff_sensor_data_index], sizeof(Dataset_Fusion[buff_sensor_data_index]), &byteswritten_SD);
  75. status_SD |= f_write(&File_SD, eol, sizeof(eol), &byteswritten_SD);
  76. if(status_SD !=FR_OK){
  77. Error_SD(3);
  78. }
  79. //}
  80. sdTime = millis() - tmp;
  81. f_sync(&File_SD);
  82. }
  83. /**-----------------------------------------------------*/