CLEON  Version 1
Cloud-Offloaded GPS Receiver
app_log.c
Go to the documentation of this file.
1 
7 #include "cleon_conf.h"
8 #include "app_define.h"
9 #include "sys_define.h"
10 #include "hal_define.h"
11 #include "fs_define.h"
12 
13 // CLEON data structure
17 
18 // Second and millisecond time tick
21 
22 // File system variables
23 extern FIL fileObject;
24 extern UINT bw;
25 char sFilename[50];
26 
27 // Flags
28 extern bool bFLAG_MicroSDInserted;
29 extern bool bFLAG_DMATransferCompleted;
30 extern bool bFLAG_IsTimeSynced;
34 
35 // Measuring time taken by each step of CLEON data logging
37 
38 // Counting number of samples stored in a file
40 
41 // User parameters
42 unsigned long ulSampleCount = 0;
43 unsigned long ulSampleGap = 0;
44 unsigned long ulChunkCount = 0;
45 unsigned long ulChunkGap = 0;
46 
47 /*----------------------------------------------------------------------------*/
55 void APP_Log(void)
56 {
58 #if TIME_MEASUREMENT == _ENABLE_
59  // Logging current system time tick
61  stTimeMeasurement[ulNumberOfCLEONSamplesInAFileCount].ullTimeAtStartAPPLog = uniSecondTimeTick.ullSecondTimeTick + uniMillisecondTimeTick.ullMillisecondTimeTick;
62  }
63 #endif
64  // Check if user button is pressed
66  // Check if time information is synchorinized
68  // Check if MicroSD is inserted
70  // Check if defined interval period has passed
73 
75 #if APP_LOG_LED_INDICATION == _ENABLE_
76  // Turn LED2 on
78 #endif // APP_LOG_LED_INDICATION
79 
80  // Start GPS
82 
84 #if APP_LOG_TEMP_AND_HUM_SENSORS == _ENABLE_
85  // Turn SHT11 on
89  // Turn SHT11 off
91 #endif // APP_LOG_TEMP_AND_HUM_SENSORS
92 
94 #if APP_LOG_LIGHT_SENSORS == _ENABLE_
95  // Start ADC conversion
97  uniCLEONSensorData.stSensors.uiLightPAR = SYS_SENSORS_S1087_Read();
98  uniCLEONSensorData.stSensors.uiLightTSR = SYS_SENSORS_S108701_Read();
99  // Stop ADC conversion
100  HAL_ADC_StopADC();
101 #endif // APP_LOG_LIGHT_SENSORS
102 
104  // Declare variables for temporary time information
105  unsigned long long ullPreviousTimeTick = 0;
106  unsigned long long ullCurrentTimeTick = 0;
107 
108  // Wait until GPS is fully up and running
109  SYS_GPS_TimingDealy(100); // 100ms delay
110 
111  // If this is the first time to log, create a file that captured samples will be written to
113  if(ulSampleCount != 1){
114  // Initailize filename
115  memset(sFilename, 0, sizeof(sFilename));
116  // File name will start with CLEON's current system time
117 #if (APP_LOG_TEMP_AND_HUM_SENSORS == _ENABLE_) || (APP_LOG_LIGHT_SENSORS == _ENABLE_)
118  sprintf(sFilename, "%lld_s16368_if4092000_m2_i2_s8.bin", uniSecondTimeTick.ullSecondTimeTick + uniMillisecondTimeTick.ullMillisecondTimeTick);
119 #else
120  sprintf(sFilename, "%lld_s16368_if4092000_m2_i2_s0.bin", uniSecondTimeTick.ullSecondTimeTick + uniMillisecondTimeTick.ullMillisecondTimeTick);
121 #endif
122  // The created file will remain openned until the number of samples defined in 'APP_LOG_SAMPLES_IN_A_FILE' are captured
123  FS_Open(&fileObject, sFilename, FA_WRITE | FA_CREATE_ALWAYS);
124  }
125  }
126 
127  // Number of GPS samples in a CLEON sample
129 
130  // Chunk gap information
131  if(ulChunkCount == 1){
132  uniCLEONHeader.stCLEONHeader.uniChunkGapMS.ulOriginalVar = 0;
133  }else{
135  }
136 
137  // Update time tick information
138  uniCLEONHeader.stCLEONHeader.uniMillisecondTimeTick.ullMillisecondTimeTick = uniSecondTimeTick.ullSecondTimeTick + uniMillisecondTimeTick.ullMillisecondTimeTick;
139 
140 #if TIME_MEASUREMENT == _ENABLE_
141  // Logging current system time tick
143  stTimeMeasurement[ulNumberOfCLEONSamplesInAFileCount].ullTimeAtWriteHeaderToSDCard = uniSecondTimeTick.ullSecondTimeTick + uniMillisecondTimeTick.ullMillisecondTimeTick;
144  }
145 #endif
146  // Move pointer to the end of file to append
147  FS_Lseek(&fileObject, FS_GetSizeOfFile(&fileObject));
148  // Write captured CLEON header (44 bytes) to the openned file
149  FS_Write(&fileObject, &uniCLEONHeader, sizeof(uniCLEONHeader), &bw);
150  // Flush cached information of a writing file
151  FS_Sync(&fileObject);
152 
154  for(int i = 0 ; i < ulChunkCount ; i++){
155  // Check mutex for GPS DMA transfer
157  // P(bFLAG_GPSSignalIsBeingCaptured)
159 
160  if( i == 0 ){
161 #if TIME_MEASUREMENT == _ENABLE_
162  // Logging current system time tick
164  stTimeMeasurement[ulNumberOfCLEONSamplesInAFileCount].ullTimeAtFirstChunk = uniSecondTimeTick.ullSecondTimeTick + uniMillisecondTimeTick.ullMillisecondTimeTick;
165  }
166 #endif
167  // First sample will be captured immediately
168  ullCurrentTimeTick = uniSecondTimeTick.ullSecondTimeTick + uniMillisecondTimeTick.ullMillisecondTimeTick;
169  ullPreviousTimeTick = ullCurrentTimeTick;
170  }else{
171  while(1){
172  ullCurrentTimeTick = uniSecondTimeTick.ullSecondTimeTick + uniMillisecondTimeTick.ullMillisecondTimeTick;
173  // From second chunk, it will wait until time defined in 'uiChunkGap' has passed
174  if(((ullPreviousTimeTick/10000) + ulChunkGap) <= (ullCurrentTimeTick/10000)){
175  ullPreviousTimeTick = ullCurrentTimeTick;
176  break;
177  }
178  }
179  }
180 
181  // Start DMA transfer
182  DMA0CTL |= DMAEN;
183 
184  // Wait until DMA-transfer-complete-IF is set
187 
188  // Move pointer to the end of file to append
189  FS_Lseek(&fileObject, FS_GetSizeOfFile(&fileObject));
190  // Write captured CLEON sample to the openned file
191  FS_Write(&fileObject, &uniCLEONGPSData, sizeof(uniCLEONGPSData), &bw);
192  // Flush cached information of a writing file
193  FS_Sync(&fileObject);
194 
195  // V(bFLAG_GPSSignalIsBeingCaptured)
197  }
198  }
199 
201 #if (APP_LOG_TEMP_AND_HUM_SENSORS == _ENABLE_) || (APP_LOG_LIGHT_SENSORS == _ENABLE_)
202  // Move pointer to the end of file to append
203  FS_Lseek(&fileObject, FS_GetSizeOfFile(&fileObject));
204  // Write captured CLEON sample to the openned file
205  FS_Write(&fileObject, &uniCLEONSensorData, sizeof(uniCLEONSensorData), &bw);
206  // Flush cached information of a writing file
207  FS_Sync(&fileObject);
208 #endif
209 
211  // Stop GPS
213 
214  // Increase the total number of samples written to the openned file
220  }
221 
222  // If the total number of captured and written samples reach value defined in 'APP_LOG_SAMPLES_IN_A_FILE',
223  // close file and reset counter to zero
225  FS_Close(&fileObject);
227  }
228 
229 #if APP_LOG_LED_INDICATION == _ENABLE_
230  // Turn LED2 off
232 #endif // APP_LOG_LED_INDICATION
233  }
234  }
235  }
236 #if TIME_MEASUREMENT == _ENABLE_
238  for(int i = 0 ; i < NUMBER_OF_TIME_MEASUREMENT_TEST; i++){
239  /*
240  if(i == 0 ){
241  printf("Sample #\n");
242  printf("Time at APP_Log start - Time at user button press\n");
243  printf("Time at start writing header to SD card - Time at APP_Log start\n");
244  printf("Time at the first GPS signal chunk - Time at start writing header to SD card\n\n");
245  }
246  */
247  printf("Sample %d\n", i);
248  printf("%lld ms\n", (stTimeMeasurement[i].ullTimeAtStartAPPLog - stTimeMeasurement[i].ullTimeAtUserButtonPressed) / 10000);
249  printf("%lld ms\n", (stTimeMeasurement[i].ullTimeAtWriteHeaderToSDCard - stTimeMeasurement[i].ullTimeAtStartAPPLog) / 10000);
250  printf("%lld ms\n\n", (stTimeMeasurement[i].ullTimeAtFirstChunk - stTimeMeasurement[i].ullTimeAtWriteHeaderToSDCard) / 10000);
251  }
252  }
253 #endif
254 
255  // If sensing is set to on-demand mode, clear the flag
256  // This locates at the end of the fuction 'APP_Log()' to prevent the flag from being set again during the function is in operation
258  }
259 }
260 
261 
262 
263 
264 
265 
266 
267 
268 
269 
270 
271 
272