sd_card_logger.cpp
Go to the documentation of this file.
1 /*
2 MIT LICENSE
3 
4 Copyright 2014-2019 Inertial Sense, Inc. - http://inertialsense.com
5 
6 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files(the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions :
7 
8 The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
9 
10 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT, IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
11 */
12 
13 #include <asf.h>
14 #include "../../../src/ISUtilities.h"
15 #include "../../../src/ISLogger.h"
16 #include "globals.h"
17 #include "ISLogFileFatFs.h"
18 #include "communications.h"
19 #include "sd_mmc_mem.h"
20 
21 #include "sd_card_logger.h"
22 
23 #undef printf
24 #define printf(...)
25 #define printf_mutex(...)
26 
28 
29 static FATFS s_fs;
30 static bool s_sd_card_ready=false;
31 static int s_enableLogger=0; // 1=enable, -1=disable
32 
33 #if UBLOX_LOG_ENABLE
34 static cISLogFileBase *s_ubloxLog = NULLPTR;
35 #endif
36 
37 
38 void update_led_log(void)
39 {
40  if(s_sd_card_ready)
41  { // SD ready
42 
43  if(g_loggerEnabled)
44  { // Logging
45  if(g_indicateFsWriteMs > 0)
46  { // writing to disk
49 // ioport_set_pin_level(SKI_BOX_STATUS_LED_PIN,IOPORT_PIN_LEVEL_HIGH); // LED ON
50  }
51  else
52  { // not writing to disk
53  LED_LOG_OFF();
54 // ioport_set_pin_level(SKI_BOX_STATUS_LED_PIN,IOPORT_PIN_LEVEL_LOW); // LED OFF
55  }
56  }
57  else
58  { // Not logging
59  if(g_gpsTimeSync)
60  { // w/ GPS
61  LED_LOG_GREEN();
62  }
63  else
64  { // No GPS
65  LED_LOG_BLUE();
66  }
67  }
68  }
69  else
70  { // SD not ready
71  LED_LOG_OFF();
72  }
73 }
74 
75 
76 static void start_logger(cISLogger& logger, is_comm_instance_t &comm)
77 {
78  if( s_sd_card_ready==false )
79  {
80  return;
81  }
82 
83  g_loggerEnabled = true;
86 
87 // uINS0_stream_stop_all(comm);
89 
90 // logger.InitSave(LOGTYPE_DAT, cISLogger::g_emptyString, 1, 0.5f, 1024 * 1024 * 5, 131072);
91  logger.InitSave(cISLogger::LOGTYPE_DAT, "IS_logs", 1, 0.5f, 1024 * 1024 * 5, 16384);
92 // logger.InitSave();
93  logger.SetDeviceInfo(&g_msg.uInsInfo); // set uINS serial number
94  logger.EnableLogging(true);
95 }
96 
97 
98 static void stop_logger(cISLogger& logger, is_comm_instance_t &comm)
99 {
100  g_loggerEnabled = false;
102  update_led_log();
103 
104 // uINS0_stream_stop_all(comm);
105 // uINS0_stream_enable_std(comm);
106 
107  logger.EnableLogging(false);
108  logger.CloseAllFiles();
109 
110 #if UBLOX_LOG_ENABLE
111  if (s_ubloxLog != NULLPTR)
112  {
113  delete s_ubloxLog;
114  s_ubloxLog = NULLPTR;
115  }
116 #endif
117 }
118 
119 
120 void enable_logger(bool enable)
121 {
122  if(enable)
123  {
124  s_enableLogger = 1;
125  }
126  else
127  { // Disable logger
128  s_enableLogger = -1;
129  }
130 }
131 
132 
134 {
135  switch(s_enableLogger)
136  {
137  case 1: // Enable logger
138  s_enableLogger = 0;
139  start_logger(logger, comm);
140  break;
141  case -1: // Disable logger
142  s_enableLogger = 0;
143  stop_logger(logger, comm);
144  }
145 }
146 
147 
148 
149 void log_ublox_raw_to_SD(cISLogger& logger, uint8_t *dataPtr, uint32_t dataSize)
150 {
151 #if UBLOX_LOG_ENABLE
152 
153  if(g_loggerEnabled)
154  { // log raw u-blox, rtcm, etc.
155  if (s_ubloxLog == NULLPTR)
156  {
157  std::string ubloxFile = (logger.LogDirectory() + "/raw_ublox.ubx");
158  s_ubloxLog = new cISLogFileFatFs(ubloxFile.c_str(), "wb");
159  }
160  if (dataSize <= MAX_DATASET_SIZE)
161  {
162  s_ubloxLog->write(dataPtr, dataSize);
163  }
164  }
165 
166 #endif
167 }
168 
169 
171 {
172  static uint32_t timeOfWeekMsLast=0;
173 
174  // Real-time clock (RTC) time synchronization with uINS
175  if(g_status.week != 0 && g_status.timeOfWeekMs!=timeOfWeekMsLast)
176  {
177  timeOfWeekMsLast = g_status.timeOfWeekMs;
178  int32_t gpsSeconds = g_status.timeOfWeekMs/1000;
179  convertMjdToDate( convertGpsToMjd(g_msg.ins2.week, gpsSeconds), (int32_t*)&g_gps_date_time.year, (int32_t*)&g_gps_date_time.month, (int32_t*)&g_gps_date_time.day);
180  convertGpsToHMS( gpsSeconds, (int32_t*)&g_gps_date_time.hour, (int32_t*)&g_gps_date_time.minute, (int32_t*)&g_gps_date_time.second );
181 
182 #if USE_RTC_DATE_TIME // use RTC
183  date_time_t rtc;
184  rtc_get_date(RTC, &rtc.year, &rtc.month, &rtc.day, &rtc.week);
185  rtc_get_time(RTC, &rtc.hour, &rtc.minute, &rtc.second);
186 
187  if( g_gps_date_time.year != rtc.year ||
188  g_gps_date_time.month != rtc.month ||
189  g_gps_date_time.day != rtc.day ||
190  g_gps_date_time.hour != rtc.hour ||
191  g_gps_date_time.minute != rtc.minute )
192  {
194 
197  }
198  else
199 #endif
200  {
201  g_gpsTimeSync = true;
202  }
203  }
204 }
205 
206 
208 {
209  static Ctrl_status statusLast = CTRL_NO_PRESENT;
211 // FRESULT res;
212 
213  if( status != statusLast )
214  { // Event
215  statusLast = status;
216 
217  switch(status)
218  {
219  case CTRL_GOOD: // Card inserted and ready
220  s_sd_card_ready = true;
222  break;
223 
224  case CTRL_NO_PRESENT: // Card removed
225  s_sd_card_ready = false;
227  printf_mutex("Card removed\r\n");
228  break;
229  }
230  }
231 }
232 
233 
235 {
236  /* Initialize SD MMC stack */
237  sd_mmc_init();
238 
239  /* Initialize FatFs handle: g_fs */
240  memset(&s_fs, 0, sizeof(FATFS));
242 
243  /* Init real-time clock: RTC configuration, 24-hour mode */
245 
246  /* Init real-time timer */
247  time_init();
248 
249 #if USE_RTC_DATE_TIME
250  /* Init real-time clock */
251  if((RTC->RTC_CR & (RTC_CR_UPDTIM | RTC_CR_UPDCAL)) != 0) //Make sure RTC is running
252  RTC->RTC_CR &= ~(RTC_CR_UPDTIM | RTC_CR_UPDCAL);
253 
254  rtc_set_time(RTC, 1, 1, 1); // hour, minute, second
255  rtc_set_date(RTC, 2018, 1, 1, dateToWeekDay(2018, 1, 1)); // year, month, day, week
256 #endif
257 }
uint32_t rtc_set_time(Rtc *p_rtc, uint32_t ul_hour, uint32_t ul_minute, uint32_t ul_second)
Set the RTC time value.
Definition: rtc.c:191
void CloseAllFiles()
Definition: ISLogger.cpp:439
bool g_gpsTimeSync
Definition: globals.c:31
#define LUN_ID_SD_MMC_0_MEM
Definition: conf_access.h:141
bool InitSave(eLogType logType=LOGTYPE_DAT, const string &directory=g_emptyString, int numDevices=1, float maxDiskSpacePercent=0.5f, uint32_t maxFileSize=1024 *1024 *5, bool useSubFolderTimestamp=true)
Definition: ISLogger.cpp:206
dev_info_t uInsInfo
Definition: globals.h:43
#define LED_LOG_OFF()
Definition: user_board.h:270
void sd_card_maintenance(void)
f
uint32_t evbStatus
Definition: data_sets.h:2952
Ctrl_status sd_mmc_test_unit_ready(uint8_t slot)
Tests the memory state and initializes the memory if required.
Definition: sd_mmc_mem.c:58
uint32_t second
Definition: globals.h:38
#define LED_LOG_YELLOW()
Definition: user_board.h:267
uint32_t dateToWeekDay(uint32_t ul_year, uint32_t ul_month, uint32_t ul_day)
evb_rtos_info_t g_rtos
Definition: globals.c:27
void rtc_get_time(Rtc *p_rtc, uint32_t *pul_hour, uint32_t *pul_minute, uint32_t *pul_second)
Get the RTC time value.
Definition: rtc.c:146
Definition: ff.h:78
#define RTC_CR_UPDCAL
(RTC_CR) Update Request Calendar Register
static void stop_logger(cISLogger &logger, is_comm_instance_t &comm)
static int s_enableLogger
rtos_task_t task[UINS_RTOS_NUM_TASKS]
Definition: data_sets.h:3335
int g_indicateFsWriteMs
ins_2_t ins2
Definition: globals.h:44
uint32_t timeOfWeekMs
Definition: data_sets.h:2946
uint32_t week
Definition: data_sets.h:2943
uint32_t week
Definition: CAN_comm.h:26
#define NULLPTR
Definition: ISConstants.h:426
static bool s_sd_card_ready
void EnableLogging(bool enabled)
Definition: ISLogger.h:76
#define printf_mutex(...)
uint32_t month
Definition: globals.h:38
uint32_t hour
Definition: globals.h:38
evb_status_t g_status
Definition: globals.c:21
void time_init(void)
Definition: d_time.c:40
string LogDirectory()
Definition: ISLogger.h:81
uint32_t rtc_set_date(Rtc *p_rtc, uint32_t ul_year, uint32_t ul_month, uint32_t ul_day, uint32_t ul_week)
Set the RTC date.
Definition: rtc.c:347
void uINS_stream_enable_PPD(is_comm_instance_t &comm)
void convertMjdToDate(int32_t mjd, int32_t *year, int32_t *month, int32_t *day)
#define RTC_CR_UPDTIM
(RTC_CR) Update Request Time Register
date_time_t g_gps_date_time
Definition: globals.c:28
virtual std::size_t write(const void *bytes, std::size_t len)=0
uint32_t minute
Definition: globals.h:38
Ctrl_status
Status returned by CTRL_ACCESS interfaces.
Definition: ctrl_access.h:74
uint32_t periodMs
Definition: data_sets.h:3301
Success, memory ready.
Definition: ctrl_access.h:76
uint32_t year
Definition: globals.h:38
static void start_logger(cISLogger &logger, is_comm_instance_t &comm)
Memory unplugged.
Definition: ctrl_access.h:78
void update_led_log(void)
uint32_t week
Definition: globals.h:38
void step_logger_control(cISLogger &logger, is_comm_instance_t &comm)
void sd_card_logger_init(void)
void enable_logger(bool enable)
bool g_loggerEnabled
Definition: globals.c:33
bool SetDeviceInfo(const dev_info_t *info, unsigned int device=0)
Definition: ISLogger.cpp:520
static FATFS s_fs
uint32_t day
Definition: globals.h:38
void rtc_get_date(Rtc *p_rtc, uint32_t *pul_year, uint32_t *pul_month, uint32_t *pul_day, uint32_t *pul_week)
Get the RTC date value.
Definition: rtc.c:296
void rtc_set_hour_mode(Rtc *p_rtc, uint32_t ul_mode)
Set the RTC hour mode.
Definition: rtc.c:77
void time_sync_from_uINS(void)
Autogenerated API include file for the Atmel Software Framework (ASF)
FRESULT f_mount(BYTE vol, FATFS *fs)
Definition: ff.c:2211
int32_t convertGpsToMjd(int32_t gpsWeek, int32_t gpsSeconds)
void log_ublox_raw_to_SD(cISLogger &logger, uint8_t *dataPtr, uint32_t dataSize)
CTRL_ACCESS interface for common SD/MMC stack.
#define MAX_DATASET_SIZE
Definition: ISComm.h:91
evb_msg_t g_msg
Definition: globals.c:25
void convertGpsToHMS(int32_t gpsSeconds, int32_t *hour, int32_t *minutes, int32_t *seconds)
#define RTC
(RTC ) Base Address
Definition: same70j19.h:536
#define LED_LOG_GREEN()
Definition: user_board.h:264
void sd_mmc_init(void)
Initialize the SD/MMC stack and low level driver required.
Definition: sd_mmc.c:1746
#define LED_LOG_BLUE()
Definition: user_board.h:265


inertial_sense_ros
Author(s):
autogenerated on Sat Sep 19 2020 03:19:05