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 "user_interface.h"
20 #include "sd_mmc_mem.h"
21 
22 #include "sd_card_logger.h"
23 
24 #undef printf
25 #define printf(...)
26 #define printf_mutex(...)
27 
29 
30 static FATFS s_fs;
31 static bool s_sd_card_ready=false;
32 static int s_enableLogger=0; // 1=enable, -1=disable
33 
34 #if UBLOX_LOG_ENABLE
35 static cISLogFileBase *s_ubloxLog = NULLPTR;
36 #endif
37 
38 
39 void refresh_led_log(void)
40 {
41  if(s_sd_card_ready)
42  { // SD ready
43 
44  if(g_loggerEnabled)
45  { // Logging
46  if(g_indicateFsWriteMs > 0)
47  { // writing to disk
50 // ioport_set_pin_level(SKI_BOX_STATUS_LED_PIN,IOPORT_PIN_LEVEL_HIGH); // LED ON
51  }
52  else
53  { // not writing to disk
54  LED_LOG_OFF();
55 // ioport_set_pin_level(SKI_BOX_STATUS_LED_PIN,IOPORT_PIN_LEVEL_LOW); // LED OFF
56  }
57  }
58  else
59  { // Not logging
60  if(g_gpsTimeSync)
61  { // w/ GPS
62  LED_LOG_GREEN();
63  }
64  else
65  { // No GPS
66  LED_LOG_BLUE();
67  }
68  }
69  }
70  else
71  { // SD not ready
72  LED_LOG_OFF();
73  }
74 }
75 
76 
77 static void start_logger(cISLogger& logger, is_comm_instance_t &comm)
78 {
79  if( s_sd_card_ready==false )
80  {
81  return;
82  }
83 
84  g_loggerEnabled = true;
87 
88 // logger.InitSave(LOGTYPE_DAT, cISLogger::g_emptyString, 1, 0.5f, 1024 * 1024 * 5, 131072);
89  logger.InitSave(cISLogger::LOGTYPE_DAT, "IS_logs", 1, 0.5f, 1024 * 1024 * 5, 16384);
90 // logger.InitSave();
91  logger.SetDeviceInfo(&g_msg.uInsInfo); // set uINS serial number
92  logger.EnableLogging(true);
93 }
94 
95 
96 static void stop_logger(cISLogger& logger, is_comm_instance_t &comm)
97 {
98  g_loggerEnabled = false;
101 
102  logger.EnableLogging(false);
103  logger.CloseAllFiles();
104 
105 #if UBLOX_LOG_ENABLE
106  if (s_ubloxLog != NULLPTR)
107  {
108  delete s_ubloxLog;
109  s_ubloxLog = NULLPTR;
110  }
111 #endif
112 }
113 
114 
115 void enable_logger(bool enable)
116 {
117  if(enable)
118  {
119  s_enableLogger = 1;
120  }
121  else
122  { // Disable logger
123  s_enableLogger = -1;
124  }
125 }
126 
127 
129 {
130  switch(s_enableLogger)
131  {
132  case 1: // Enable logger
133  s_enableLogger = 0;
134  start_logger(logger, comm);
135  break;
136  case -1: // Disable logger
137  s_enableLogger = 0;
138  stop_logger(logger, comm);
139  }
140 }
141 
142 
143 
144 void log_ublox_raw_to_SD(cISLogger& logger, uint8_t *dataPtr, uint32_t dataSize)
145 {
146 #if UBLOX_LOG_ENABLE
147 
148  if(g_loggerEnabled)
149  { // log raw u-blox, rtcm, etc.
150  if (s_ubloxLog == NULLPTR)
151  {
152  std::string ubloxFile = (logger.LogDirectory() + "/raw_ublox.ubx");
153  s_ubloxLog = new cISLogFileFatFs(ubloxFile.c_str(), "wb");
154  }
155  if (dataSize <= MAX_DATASET_SIZE)
156  {
157  s_ubloxLog->write(dataPtr, dataSize);
158  }
159  }
160 
161 #endif
162 }
163 
164 
166 {
167  static uint32_t timeOfWeekMsLast=0;
168 
169  // Real-time clock (RTC) time synchronization with uINS
170  if(g_status.week != 0 && g_status.timeOfWeekMs!=timeOfWeekMsLast)
171  {
172  timeOfWeekMsLast = g_status.timeOfWeekMs;
173  int32_t gpsSeconds = g_status.timeOfWeekMs/1000;
174  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);
175  convertGpsToHMS( gpsSeconds, (int32_t*)&g_gps_date_time.hour, (int32_t*)&g_gps_date_time.minute, (int32_t*)&g_gps_date_time.second );
176 
177 #if USE_RTC_DATE_TIME // use RTC
178  date_time_t rtc;
179  rtc_get_date(RTC, &rtc.year, &rtc.month, &rtc.day, &rtc.week);
180  rtc_get_time(RTC, &rtc.hour, &rtc.minute, &rtc.second);
181 
182  if( g_gps_date_time.year != rtc.year ||
183  g_gps_date_time.month != rtc.month ||
184  g_gps_date_time.day != rtc.day ||
185  g_gps_date_time.hour != rtc.hour ||
186  g_gps_date_time.minute != rtc.minute )
187  {
189 
192  }
193  else
194 #endif
195  {
196  g_gpsTimeSync = true;
197  }
198  }
199 }
200 
201 
203 {
204  static Ctrl_status statusLast = CTRL_NO_PRESENT;
206 // FRESULT res;
207 
208  if( status != statusLast )
209  { // Event
210  statusLast = status;
211 
212  switch(status)
213  {
214  case CTRL_GOOD: // Card inserted and ready
215  s_sd_card_ready = true;
217  break;
218 
219  case CTRL_NO_PRESENT: // Card removed
220  s_sd_card_ready = false;
222  printf_mutex("Card removed\r\n");
223  break;
224  }
225  }
226 }
227 
228 
230 {
231  /* Initialize SD MMC stack */
232  sd_mmc_init();
233 
234  /* Initialize FatFs handle: g_fs */
235  memset(&s_fs, 0, sizeof(FATFS));
237 
238  /* Init real-time clock: RTC configuration, 24-hour mode */
240 
241  /* Init real-time timer */
242  time_init();
243 
244 #if USE_RTC_DATE_TIME
245  /* Init real-time clock */
246  if((RTC->RTC_CR & (RTC_CR_UPDTIM | RTC_CR_UPDCAL)) != 0) //Make sure RTC is running
247  RTC->RTC_CR &= ~(RTC_CR_UPDTIM | RTC_CR_UPDCAL);
248 
249  rtc_set_time(RTC, 1, 1, 1); // hour, minute, second
250  rtc_set_date(RTC, 2018, 1, 1, dateToWeekDay(2018, 1, 1)); // year, month, day, week
251 #endif
252 }
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:45
#define LED_LOG_OFF()
Definition: user_board.h:270
void sd_card_maintenance(void)
uint32_t evbStatus
Definition: data_sets.h:3069
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:40
#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:3457
int g_indicateFsWriteMs
void evbUiRefreshLedLog()
ins_2_t ins2
Definition: globals.h:46
uint32_t timeOfWeekMs
Definition: data_sets.h:3063
uint32_t week
Definition: data_sets.h:3060
uint32_t week
Definition: CAN_comm.h:26
#define NULLPTR
Definition: ISConstants.h:426
static bool s_sd_card_ready
void refresh_led_log(void)
void EnableLogging(bool enabled)
Definition: ISLogger.h:76
#define printf_mutex(...)
uint32_t month
Definition: globals.h:40
uint32_t hour
Definition: globals.h:40
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 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:40
Ctrl_status
Status returned by CTRL_ACCESS interfaces.
Definition: ctrl_access.h:74
uint32_t periodMs
Definition: data_sets.h:3423
Success, memory ready.
Definition: ctrl_access.h:76
uint32_t year
Definition: globals.h:40
static void start_logger(cISLogger &logger, is_comm_instance_t &comm)
Memory unplugged.
Definition: ctrl_access.h:78
uint32_t week
Definition: globals.h:40
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:40
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 Sun Feb 28 2021 03:17:58